use txt2tex::parser::parse_content;
use txt2tex::template::render;

// ── parser tests ──────────────────────────────────────────────

#[test]
fn test_name_is_parsed() {
    let input = "# PERSONAL INFORMATION\nname: Alex\nemail: alex@test.com\n";
    let resume = parse_content(input).unwrap();
    assert_eq!(resume.name, "Alex");
}

#[test]
fn test_email_is_parsed() {
    let input = "# PERSONAL INFORMATION\nname: Alex\nemail: alex@test.com\n";
    let resume = parse_content(input).unwrap();
    assert_eq!(resume.email, "alex@test.com");
}

#[test]
fn test_skills_parsed_with_categories() {
    let input = "# SKILLS\nLanguages: Rust, C++, Python\nFrameworks: React, Node.js\n";
    let resume = parse_content(input).unwrap();
    assert_eq!(resume.skills.len(), 2);
    assert_eq!(resume.skills[0].category, "Languages");
    assert_eq!(resume.skills[0].items.len(), 3);
    assert_eq!(resume.skills[1].category, "Frameworks");
}

#[test]
fn test_project_is_parsed() {
    let input = "\
# PROJECTS
project:
title: MyApp
description: A cool app I built
tech: Rust, Node.js
";
    let resume = parse_content(input).unwrap();
    assert_eq!(resume.projects.len(), 1);
    assert_eq!(resume.projects[0].title, "MyApp");
    assert_eq!(resume.projects[0].tech[0], "Rust");
    assert_eq!(resume.projects[0].tech[1], "Node.js");
}

#[test]
fn test_experience_bullets_parsed() {
    let input = "\
# EXPERIENCE
experience:
company: TechCorp
role: Intern
duration: Summer 2024
- Built REST APIs handling 10k requests per day
- Reduced latency by 30%
";
    let resume = parse_content(input).unwrap();
    assert_eq!(resume.experience[0].company, "TechCorp");
    assert_eq!(resume.experience[0].details.len(), 2);
    assert_eq!(resume.experience[0].details[0], "Built REST APIs handling 10k requests per day");
}

#[test]
fn test_multiple_projects_parsed() {
    let input = "\
# PROJECTS
project:
title: Project A
description: First project
tech: Rust

project:
title: Project B
description: Second project
tech: Python
";
    let resume = parse_content(input).unwrap();
    assert_eq!(resume.projects.len(), 2);
    assert_eq!(resume.projects[0].title, "Project A");
    assert_eq!(resume.projects[1].title, "Project B");
}

#[test]
fn test_is_valid_with_name_and_email() {
    let input = "# PERSONAL INFORMATION\nname: Alex\nemail: alex@test.com\n";
    let resume = parse_content(input).unwrap();
    assert!(resume.is_valid());
}

#[test]
fn test_is_valid_fails_without_email() {
    let input = "# PERSONAL INFORMATION\nname: Alex\n";
    let resume = parse_content(input).unwrap();
    assert!(!resume.is_valid());
}

#[test]
fn test_is_valid_fails_without_name() {
    let input = "# PERSONAL INFORMATION\nemail: alex@test.com\n";
    let resume = parse_content(input).unwrap();
    assert!(!resume.is_valid());
}

// ── template tests ────────────────────────────────────────────

#[test]
fn test_latex_contains_name() {
    let input = "# PERSONAL INFORMATION\nname: Alex Johnson\nemail: alex@test.com\n";
    let resume = parse_content(input).unwrap();
    let latex  = render(&resume).unwrap();
    assert!(latex.contains("Alex Johnson"));
}

#[test]
fn test_latex_has_document_structure() {
    let input = "# PERSONAL INFORMATION\nname: Alex\nemail: alex@test.com\n";
    let resume = parse_content(input).unwrap();
    let latex  = render(&resume).unwrap();
    assert!(latex.contains(r"\documentclass"));
    assert!(latex.contains(r"\begin{document}"));
    assert!(latex.contains(r"\end{document}"));
}

#[test]
fn test_latex_escapes_ampersand() {
    let input = "# PERSONAL INFORMATION\nname: John & Jane\nemail: test@test.com\n";
    let resume = parse_content(input).unwrap();
    let latex  = render(&resume).unwrap();
    // & must become \& in LaTeX otherwise it breaks table formatting
    assert!(latex.contains(r"John \& Jane"));
}

#[test]
fn test_latex_has_skills_section() {
    let input = "\
# PERSONAL INFORMATION
name: Alex
email: alex@test.com
# SKILLS
Languages: Rust, Python
";
    let resume = parse_content(input).unwrap();
    let latex  = render(&resume).unwrap();
    assert!(latex.contains("Technical Skills"));
    assert!(latex.contains("Languages"));
    assert!(latex.contains("Rust"));
}
