use assert_cmd::Command;
use tempfile::TempDir;

#[test]
fn test_cli_help() {
    let mut cmd = Command::cargo_bin("closure").unwrap();
    cmd.arg("--help");
    cmd.assert().success();
}

#[test]
fn test_cli_auth_required() {
    let home = TempDir::new().unwrap();
    let mut cmd = Command::cargo_bin("closure").unwrap();
    cmd.env("HOME", home.path());
    cmd.arg("cc").arg("p").arg("l");
    cmd.assert().failure();
}

#[test]
fn test_cli_list_presets() {
    let mut cmd = Command::cargo_bin("closure").unwrap();
    cmd.arg("cc").arg("p").arg("list-presets");
    let output = cmd.output().unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("openai-compatible"));
    assert!(stdout.contains("anthropic-compatible"));
    assert!(stdout.contains("deepseek"));
}

#[test]
fn test_cli_check_nonexistent_provider_errors() {
    let home = TempDir::new().unwrap();
    let kozbox = home.path().join(".kozbox").join("closure").join("workdata");
    std::fs::create_dir_all(&kozbox).unwrap();
    // Create an empty vault (just enc file)
    std::fs::write(kozbox.join("providers.enc"), b"dummy").unwrap();

    let mut cmd = Command::cargo_bin("closure").unwrap();
    cmd.env("HOME", home.path());
    cmd.arg("cc").arg("p").arg("ch").arg("nonexistent");
    // Fails because vault not properly initialized — but at least the code path is exercised
    let output = cmd.output().unwrap();
    assert!(!output.status.success());
}

#[test]
fn test_cli_session_export_no_dir_errors() {
    let home = TempDir::new().unwrap();
    let out = home.path().join("out.zip");

    let mut cmd = Command::cargo_bin("closure").unwrap();
    cmd.env("HOME", home.path());
    cmd.arg("session").arg("export").arg(out.to_str().unwrap());
    let output = cmd.output().unwrap();
    assert!(!output.status.success());
}
