use crate::model::Resume;
use colored::Colorize;

pub fn analyze(resume: &Resume) {
    println!("{}", "\n  Resume Analysis".cyan().bold());
    println!("{}", "  ───────────────\n".cyan());

    let mut issues = 0;
    let mut tips   = 0;

    // ── required fields ───────────────────────────────────────
    if resume.name.is_empty() {
        print_issue("Missing name");
        issues += 1;
    }
    if resume.email.is_empty() {
        print_issue("Missing email address");
        issues += 1;
    }
    if resume.phone.is_empty() {
        print_issue("Missing phone number");
        issues += 1;
    }
    if resume.github.is_empty() {
        print_tip("Add your GitHub link — recruiters check this");
        tips += 1;
    }

    // ── education ─────────────────────────────────────────────
    if resume.degree.is_empty() {
        print_issue("Missing education section");
        issues += 1;
    }

    // ── skills ────────────────────────────────────────────────
    if resume.skills.is_empty() {
        print_issue("No skills listed — add a Technical Skills section");
        issues += 1;
    } else if resume.skills.len() == 1 {
        print_tip("Add more skill categories (Frameworks, Developer Tools, Libraries)");
        tips += 1;
    }

    // ── projects ──────────────────────────────────────────────
    if resume.projects.is_empty() {
        print_issue("No projects listed — add at least 2 projects");
        issues += 1;
    } else {
        for proj in &resume.projects {
            if proj.description.is_empty() {
                print_issue(&format!(
                    "Project '{}' has no description", proj.title
                ));
                issues += 1;
            } else if proj.description.len() < 30 {
                print_tip(&format!(
                    "Project '{}' description is very short — add more detail",
                    proj.title
                ));
                tips += 1;
            }

            if proj.tech.is_empty() {
                print_issue(&format!(
                    "Project '{}' has no tech stack listed", proj.title
                ));
                issues += 1;
            }
        }
    }

    // ── experience ────────────────────────────────────────────
    if resume.experience.is_empty() {
        print_tip("No experience listed — add internships, open source, or part-time work");
        tips += 1;
    } else {
        for exp in &resume.experience {
            if exp.details.is_empty() {
                print_issue(&format!(
                    "Experience at '{}' has no bullet points — add what you did",
                    exp.company
                ));
                issues += 1;
            }

            for detail in &exp.details {
                // good bullet points have numbers — they show real impact
                // e.g. "handled 10k requests" not just "handled requests"
                let has_number = detail.chars().any(|c| c.is_ascii_digit());
                if !has_number {
                    print_tip(&format!(
                        "Add a number/metric to: \"{}...\"",
                        &detail[..detail.len().min(40)]
                    ));
                    tips += 1;
                }
            }
        }
    }

    // ── summary ───────────────────────────────────────────────
    println!();

    if issues == 0 && tips == 0 {
        println!("  {}  Resume looks great!", "✓".green().bold());
    } else {
        if issues > 0 {
            println!(
                "  {}  {} issue{} found — fix these first",
                "✗".red().bold(),
                issues,
                if issues == 1 { "" } else { "s" }
            );
        }
        if tips > 0 {
            println!(
                "  {}  {} suggestion{} to make it stronger",
                "→".yellow().bold(),
                tips,
                if tips == 1 { "" } else { "s" }
            );
        }
    }

    println!();
}

fn print_issue(msg: &str) {
    println!("  {}  {}", "✗".red().bold(), msg);
}

fn print_tip(msg: &str) {
    println!("  {}  {}", "→".yellow(), msg);
}
