mod common;

use assert_cmd::cargo::CommandCargoExt;
use std::process::Command;
use tempfile::TempDir;

#[test]
fn list_refuses_without_vault_marker() {
    let dir = TempDir::new().unwrap();
    // No .relicario/ in dir — list should bail with a friendly error.
    let mut cmd = Command::cargo_bin("relicario").unwrap();
    let out = cmd.current_dir(dir.path())
        .env("RELICARIO_TEST_PASSPHRASE", "foo")
        .arg("list")
        .output()
        .unwrap();
    assert!(!out.status.success());
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(stderr.contains(".relicario"), "expected marker hint: {stderr}");
}

#[test]
fn get_finds_vault_in_parent_dir() {
    let v = common::TestVault::init();
    v.run(&["add", "login", "--title", "parent-test",
           "--username", "u", "--password", "p"]);

    // Create a nested subdir and run `list` from inside it.
    let nested = v.path().join("a/b/c");
    std::fs::create_dir_all(&nested).unwrap();

    let mut cmd = Command::cargo_bin("relicario").unwrap();
    cmd.current_dir(&nested)
        .env("RELICARIO_IMAGE", &v.reference_image)
        .env("RELICARIO_TEST_PASSPHRASE", &v.passphrase)
        .arg("list");
    let out = cmd.output().unwrap();
    assert!(out.status.success(), "list from nested dir failed: {:?}", out);
    assert!(String::from_utf8(out.stdout).unwrap().contains("parent-test"));
}

#[test]
fn v1_vault_is_rejected_with_clear_error() {
    // Synthesize an on-disk v1 vault: .idfoto/ dir with old params.json.
    // Since vault_dir detection uses .relicario/, the pre-rename dir name is
    // naturally rejected without any compat shim. Confirm that.
    let dir = TempDir::new().unwrap();
    std::fs::create_dir(dir.path().join(".idfoto")).unwrap();

    let mut cmd = Command::cargo_bin("relicario").unwrap();
    let out = cmd.current_dir(dir.path())
        .env("RELICARIO_TEST_PASSPHRASE", "foo")
        .arg("list")
        .output()
        .unwrap();
    assert!(!out.status.success());
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(stderr.contains(".relicario"), "expected relicario marker demand: {stderr}");
}
