"""Tests for toil analyzer and parser."""
from toil_tracker.parser import parse_inventory
from toil_tracker.analyzers.toil_analyzer import analyze_toil
from toil_tracker.models import (
    ToilTask, ToilCategory, TaskFrequency, AutomationPotential, Severity,
)


class TestParser:
    def test_parse_inventory(self, sample_inventory_yaml):
        tasks = parse_inventory(sample_inventory_yaml)
        assert len(tasks) == 2

    def test_parse_empty(self):
        assert parse_inventory("") == []

    def test_parse_task_fields(self, sample_inventory_yaml):
        tasks = parse_inventory(sample_inventory_yaml)
        deploy = [t for t in tasks if t.name == "Manual deployments"][0]
        assert deploy.category == ToilCategory.DEPLOYMENT
        assert deploy.frequency == TaskFrequency.DAILY
        assert deploy.team_size == 2
        assert deploy.is_automatable is True


class TestToilAnalyzer:
    def test_toil001_daily_manual(self, daily_manual_task):
        report = analyze_toil([daily_manual_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-001" in rule_ids

    def test_toil001_weekly_no_trigger(self, scaling_task):
        report = analyze_toil([scaling_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-001" not in rule_ids

    def test_toil002_high_annual(self, daily_manual_task):
        report = analyze_toil([daily_manual_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-002" in rule_ids

    def test_toil003_quick_roi(self, quick_roi_task):
        report = analyze_toil([quick_roi_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-003" in rule_ids

    def test_toil004_multi_person(self, multi_person_task):
        report = analyze_toil([multi_person_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-004" in rule_ids

    def test_toil005_incident_toil(self, incident_task):
        report = analyze_toil([incident_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-005" in rule_ids

    def test_toil006_deployment_toil(self, daily_manual_task):
        report = analyze_toil([daily_manual_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-006" in rule_ids

    def test_toil007_access_toil(self, access_task):
        report = analyze_toil([access_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-007" in rule_ids

    def test_toil008_scaling_toil(self, scaling_task):
        report = analyze_toil([scaling_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-008" in rule_ids

    def test_toil009_low_automation(self, low_auto_task):
        report = analyze_toil([low_auto_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-009" in rule_ids

    def test_toil010_budget_exceeded(self):
        """Generate enough toil to exceed 50% of capacity."""
        heavy_tasks = [
            ToilTask(name=f"toil-{i}", frequency=TaskFrequency.DAILY,
                     duration_minutes=60, team_size=1, is_automatable=True)
            for i in range(4)
        ]
        # 4 * 365h = 1460h > 2080*0.5 = 1040
        report = analyze_toil(heavy_tasks, team_capacity_hours=2080)
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-010" in rule_ids

    def test_toil010_within_budget(self, quick_roi_task):
        report = analyze_toil([quick_roi_task])
        rule_ids = [f.rule_id for f in report.findings]
        assert "TOIL-010" not in rule_ids

    def test_report_health_score(self, daily_manual_task, incident_task):
        report = analyze_toil([daily_manual_task, incident_task])
        assert report.health_score < 100

    def test_non_automatable_skipped(self):
        task = ToilTask(
            name="manual-only",
            category=ToilCategory.DEPLOYMENT,
            frequency=TaskFrequency.DAILY,
            is_automatable=False,
        )
        report = analyze_toil([task])
        rule_ids = [f.rule_id for f in report.findings]
        # TOIL-006 checks for deployment + automatable
        assert "TOIL-006" not in rule_ids
