//! Test-only helpers exported for integration tests under `tests/`.
use std::sync::{Mutex, OnceLock};

static ATHENA_KEY_TEST_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

/// `ATHENA_KEY_12` value used while [`AthAdminKeyGuard`] is held.
pub const ATHENA_TEST_ADMIN_KEY: &str = "integration-test-athena-admin-key-2026";

/// Serializes `ATHENA_KEY_12` across concurrent tests.
pub struct AthAdminKeyGuard {
    _guard: std::sync::MutexGuard<'static, ()>,
}

impl AthAdminKeyGuard {
    pub fn new() -> Self {
        let _guard = ATHENA_KEY_TEST_LOCK
            .get_or_init(|| Mutex::new(()))
            .lock()
            .expect("ATHENA_KEY_12 test lock poisoned");
        unsafe {
            std::env::set_var("ATHENA_KEY_12", ATHENA_TEST_ADMIN_KEY);
        }
        Self { _guard }
    }
}

impl Drop for AthAdminKeyGuard {
    fn drop(&mut self) {
        unsafe {
            std::env::remove_var("ATHENA_KEY_12");
        }
    }
}
