"""Tests for HTML report generation."""

import os

from yaml_doctor.models import (
    FileType,
    Finding,
    Grade,
    LintResult,
    LintRule,
    ScanResult,
    Severity,
)
from yaml_doctor.output.html_report import export_file_html, export_scan_html


def _make_result(score=85, findings_count=3, valid=True):
    lr = LintResult(file_path="test.yaml", file_type=FileType.KUBERNETES, score=score, grade=Grade.A)
    lr.valid_yaml = valid
    if not valid:
        lr.parse_error = "Unexpected token"
        lr.score = 0
        lr.grade = Grade.F
    for i in range(findings_count):
        sev = [Severity.CRITICAL, Severity.HIGH, Severity.MEDIUM][i % 3]
        rule = LintRule(f"T{i:03d}", "Test", sev, "Test rule", "Description", "Fix suggestion")
        lr.findings.append(Finding(rule=rule, line=i + 1, message=f"Issue {i}"))
    return lr


class TestExportFileHtml:
    def test_creates_file(self, tmp_path):
        out = str(tmp_path / "report.html")
        result = _make_result()
        path = export_file_html(result, out)
        assert os.path.exists(path)
        html = open(path).read()
        assert "yaml-doctor" in html
        assert "test.yaml" in html

    def test_invalid_yaml_report(self, tmp_path):
        out = str(tmp_path / "report.html")
        result = _make_result(valid=False)
        path = export_file_html(result, out)
        html = open(path).read()
        assert "PARSE ERROR" in html

    def test_findings_in_report(self, tmp_path):
        out = str(tmp_path / "report.html")
        result = _make_result(findings_count=5)
        export_file_html(result, out)
        html = open(out).read()
        assert "T000" in html
        assert "Issue 0" in html

    def test_creates_parent_dirs(self, tmp_path):
        out = str(tmp_path / "sub" / "dir" / "report.html")
        result = _make_result()
        export_file_html(result, out)
        assert os.path.exists(out)


class TestExportScanHtml:
    def test_creates_dashboard(self, tmp_path):
        out = str(tmp_path / "dashboard.html")
        scan = ScanResult(
            files=[_make_result(score=90), _make_result(score=60)],
            total_files=2,
            total_findings=6,
            overall_score=75,
            overall_grade=Grade.B,
        )
        path = export_scan_html(scan, out)
        assert os.path.exists(path)
        html = open(path).read()
        assert "yaml-doctor" in html
        assert "Scan Report" in html
        assert "Severity Breakdown" in html
        assert "File Results" in html

    def test_summary_cards(self, tmp_path):
        out = str(tmp_path / "dashboard.html")
        scan = ScanResult(
            files=[_make_result(score=95)],
            total_files=1,
            total_findings=3,
            overall_score=95,
            overall_grade=Grade.A_PLUS,
        )
        export_scan_html(scan, out)
        html = open(out).read()
        assert "Files Scanned" in html
        assert "Total Findings" in html
        assert "Passed" in html

    def test_empty_scan(self, tmp_path):
        out = str(tmp_path / "empty.html")
        scan = ScanResult()
        export_scan_html(scan, out)
        assert os.path.exists(out)
