use std::fs;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use homeboy::refactor;

fn tmp_dir(name: &str) -> PathBuf {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    std::env::temp_dir().join(format!("homeboy-refactor-{name}-{nanos}"))
}

#[test]
fn test_build_plan() {
    let root = tmp_dir("missing");
    fs::create_dir_all(&root).unwrap();

    let result = refactor::build_plan("src/missing.rs", &root, "grouped", true);
    assert!(result.is_err());

    let _ = fs::remove_dir_all(root);
}

#[test]
fn test_apply_plan_skeletons() {
    let root = tmp_dir("skeletons");
    fs::create_dir_all(&root).unwrap();

    let plan = refactor::DecomposePlan {
        file: "src/core/deploy.rs".to_string(),
        strategy: "grouped".to_string(),
        audit_safe: true,
        total_items: 2,
        groups: vec![
            refactor::DecomposeGroup {
                name: "types".to_string(),
                suggested_target: "src/core/deploy/types.inc".to_string(),
                item_names: vec!["DeployConfig".to_string()],
            },
            refactor::DecomposeGroup {
                name: "execution".to_string(),
                suggested_target: "src/core/deploy/execution.inc".to_string(),
                item_names: vec!["run".to_string()],
            },
        ],
        projected_audit_impact: refactor::DecomposeAuditImpact {
            estimated_new_files: 2,
            estimated_new_test_files: 0,
            recommended_test_files: vec![],
            likely_findings: vec![],
        },
        checklist: vec![],
        warnings: vec![],
    };

    let created = refactor::apply_plan_skeletons(&plan, &root).unwrap();
    assert_eq!(created.len(), 2);
    assert!(root.join("src/core/deploy/types.inc").exists());
    assert!(root.join("src/core/deploy/execution.inc").exists());

    let _ = fs::remove_dir_all(root);
}
