//! Dormant ADR advisory check.

use koala_core::invariant::Context;
use koala_drift::checks::adr_dormancy::AdrDormancy;
use koala_drift::{Check, FindingKind, Severity};
use std::fs;
use tempfile::TempDir;

fn write(dir: &std::path::Path, rel: &str, body: &str) {
    let p = dir.join(rel);
    fs::create_dir_all(p.parent().unwrap()).unwrap();
    fs::write(p, body).unwrap();
}

fn adr(id: &str, status: &str) -> String {
    format!("---\nid: {id}\ntitle: t\nstatus: {status}\ndate: 2026-01-01\n---\n\nbody\n")
}

#[test]
fn dormant_accepted_adr_reports_advisory() {
    let tmp = TempDir::new().unwrap();
    write(
        tmp.path(),
        "wiki/decisions/0007-lonely.md",
        &adr("0007", "accepted"),
    );
    // No other wiki file references it.
    write(tmp.path(), "wiki/README.md", "intro\n");

    let ctx = Context::new(tmp.path().to_path_buf());
    let findings = AdrDormancy.run(&ctx);
    assert_eq!(findings.len(), 1, "{findings:#?}");
    assert_eq!(findings[0].severity, Severity::Advisory);
    assert_eq!(findings[0].kind, FindingKind::AdrDormant);
}

#[test]
fn referenced_accepted_adr_quiet() {
    let tmp = TempDir::new().unwrap();
    write(
        tmp.path(),
        "wiki/decisions/0007-pop.md",
        &adr("0007", "accepted"),
    );
    write(tmp.path(), "wiki/features/x.md", "see ADR-0007\n");

    let ctx = Context::new(tmp.path().to_path_buf());
    assert_eq!(AdrDormancy.run(&ctx).len(), 0);
}

#[test]
fn superseded_adr_not_reported() {
    let tmp = TempDir::new().unwrap();
    write(
        tmp.path(),
        "wiki/decisions/0007-old.md",
        &adr("0007", "superseded"),
    );
    // Still no inbound refs — but only `accepted` ADRs are evaluated.
    write(tmp.path(), "wiki/README.md", "intro\n");

    let ctx = Context::new(tmp.path().to_path_buf());
    assert_eq!(AdrDormancy.run(&ctx).len(), 0);
}
