use std::path::Path;

fn main() {
    // Verify that required directories exist at build time
    let required_dirs = vec!["static", "templates", "node_modules"];

    let mut missing_dirs = Vec::new();

    for dir in &required_dirs {
        if !Path::new(dir).exists() {
            missing_dirs.push(*dir);
        }
    }

    if !missing_dirs.is_empty() {
        println!("cargo:warning=Missing required directories:");
        for dir in &missing_dirs {
            println!("cargo:warning=  - {}", dir);
        }

        if missing_dirs.contains(&"node_modules") {
            println!("cargo:warning=Run 'npm install' to install frontend dependencies");
        }
    }

    // Verify critical files
    let required_files = vec![
        "templates/term.html",
        "static/terminal.js",
        "static/terminado-addon.js",
        "static/styles.css",
    ];

    let mut missing_files = Vec::new();

    for file in &required_files {
        if !Path::new(file).exists() {
            missing_files.push(*file);
        }
    }

    if !missing_files.is_empty() {
        println!("cargo:warning=Missing required files:");
        for file in &missing_files {
            println!("cargo:warning=  - {}", file);
        }
    }

    // Tell cargo to rerun if these directories change
    println!("cargo:rerun-if-changed=static/");
    println!("cargo:rerun-if-changed=templates/");
    println!("cargo:rerun-if-changed=package.json");
    println!("cargo:rerun-if-changed=package-lock.json");
}
