"""Tests for data models."""
from toil_tracker.models import (
    ToilTask, ToilReport, Finding, Severity, ToilCategory,
    TaskFrequency, AutomationPotential, TOIL_RULES,
    FREQUENCY_ANNUAL_MULTIPLIER,
)


class TestToilTask:
    def test_defaults(self):
        t = ToilTask()
        assert t.frequency == TaskFrequency.WEEKLY
        assert t.is_automatable

    def test_annual_hours_daily(self):
        t = ToilTask(frequency=TaskFrequency.DAILY, duration_minutes=60, team_size=1)
        assert t.annual_hours == 365.0

    def test_annual_hours_weekly(self):
        t = ToilTask(frequency=TaskFrequency.WEEKLY, duration_minutes=60, team_size=1)
        assert t.annual_hours == 52.0

    def test_annual_hours_team_multiplied(self):
        t = ToilTask(frequency=TaskFrequency.WEEKLY, duration_minutes=60, team_size=3)
        assert t.annual_hours == 156.0

    def test_automation_roi(self):
        t = ToilTask(frequency=TaskFrequency.WEEKLY, duration_minutes=60, team_size=1, automation_effort_hours=52)
        # 52h/year = 4.33h/month. ROI = 52/4.33 = ~12 months
        assert 11 < t.automation_roi_months < 13

    def test_roi_zero_hours(self):
        t = ToilTask(duration_minutes=0, automation_effort_hours=10)
        assert t.automation_roi_months == float("inf")


class TestToilReport:
    def test_compute_summary_empty(self):
        report = ToilReport()
        report.compute_summary()
        assert report.health_score == 100.0
        assert report.grade == "A"

    def test_compute_summary_with_findings(self):
        report = ToilReport(
            tasks=[ToilTask()],
            findings=[Finding(severity=Severity.CRITICAL), Finding(severity=Severity.HIGH)],
        )
        report.compute_summary()
        assert report.critical_count == 1
        assert report.high_count == 1
        assert report.health_score == 75.0
        assert report.grade == "C"

    def test_automation_coverage(self):
        report = ToilReport(tasks=[
            ToilTask(frequency=TaskFrequency.WEEKLY, duration_minutes=60, is_automatable=True),
            ToilTask(frequency=TaskFrequency.WEEKLY, duration_minutes=60, is_automatable=False),
        ])
        report.compute_summary()
        assert report.automation_coverage == 50.0

    def test_grade_f(self):
        report = ToilReport(findings=[Finding(severity=Severity.CRITICAL)] * 7)
        report.compute_summary()
        assert report.grade == "F"


class TestRules:
    def test_all_rules_exist(self):
        assert len(TOIL_RULES) == 10
        for i in range(1, 11):
            assert f"TOIL-{i:03d}" in TOIL_RULES

    def test_frequency_multipliers(self):
        assert FREQUENCY_ANNUAL_MULTIPLIER[TaskFrequency.DAILY] == 365
        assert FREQUENCY_ANNUAL_MULTIPLIER[TaskFrequency.WEEKLY] == 52
