use std::collections::HashMap;
use closure_app::core::provider::{Provider, ProviderStore};
use closure_app::core::store::Store;
use tempfile::TempDir;

#[test]
fn test_store_roundtrip() {
    let tmp = TempDir::new().unwrap();
    let store = Store::new(tmp.path());

    let mut env = HashMap::new();
    env.insert("ANTHROPIC_AUTH_TOKEN".to_string(), "sk-test".to_string());

    let data = ProviderStore {
        version: 1,
        providers: vec![
            Provider::ClaudeCode {
                name: "test-provider".to_string(),
                model: Some("kimi-for-coding".to_string()),
                env,
            }
        ],
    };

    store.save_encrypted(&data, "password").unwrap();
    let loaded = store.load_encrypted("password").unwrap();

    assert_eq!(loaded, data);
}

#[test]
fn test_store_load_without_auth_fails() {
    let tmp = TempDir::new().unwrap();
    let store = Store::new(tmp.path());

    let result = store.load();
    assert!(result.is_err());
}

#[test]
fn test_store_init() {
    let tmp = TempDir::new().unwrap();
    let store = Store::new(tmp.path());

    assert!(!store.has_encrypted());

    let store_data = store.init("password").unwrap();
    assert!(store.has_encrypted());
    assert!(store.is_authenticated());
    assert_eq!(store_data.providers.len(), 0);

    // Double init should fail
    let result = store.init("password2");
    assert!(result.is_err());
}
