"""Tests for sre_beacon.calculators.toil_calculator."""

import pytest

from sre_beacon.calculators.toil_calculator import (
    calculate_toil,
    load_toil_tasks,
    toil_health,
)
from sre_beacon.models import ToilFrequency, ToilTask


class TestLoadToilTasks:
    def test_load_from_yaml(self, demo_dir):
        tasks = load_toil_tasks(demo_dir["toil"])
        assert len(tasks) == 10
        assert tasks[0].frequency == ToilFrequency.DAILY


class TestCalculateToil:
    def test_basic_calculation(self, sample_toil_tasks):
        report = calculate_toil(sample_toil_tasks, engineering_hours_per_month=160)
        assert report.total_toil_hours_per_month > 0
        assert 0 < report.toil_percentage <= 100
        assert report.automatable_hours_per_month > 0

    def test_daily_task_hours(self):
        tasks = [ToilTask(name="Daily", frequency=ToilFrequency.DAILY,
                          duration_minutes=60, automatable=True, category="ops")]
        report = calculate_toil(tasks)
        # 22 working days × 60 min = 1320 min = 22 hours
        assert abs(report.total_toil_hours_per_month - 22.0) < 0.1

    def test_monthly_task_hours(self):
        tasks = [ToilTask(name="Monthly", frequency=ToilFrequency.MONTHLY,
                          duration_minutes=120, automatable=False, category="ops")]
        report = calculate_toil(tasks)
        assert abs(report.total_toil_hours_per_month - 2.0) < 0.1
        assert report.automatable_hours_per_month == 0.0

    def test_by_category(self, sample_toil_tasks):
        report = calculate_toil(sample_toil_tasks)
        assert "kubernetes" in report.by_category
        assert "security" in report.by_category
        assert report.by_category["kubernetes"]["tasks"] == 1

    def test_by_frequency(self, sample_toil_tasks):
        report = calculate_toil(sample_toil_tasks)
        assert "daily" in report.by_frequency
        assert report.by_frequency["daily"]["tasks"] == 2

    def test_automation_potential(self):
        tasks = [
            ToilTask(name="Auto", frequency=ToilFrequency.DAILY,
                     duration_minutes=60, automatable=True, category="ops"),
            ToilTask(name="Manual", frequency=ToilFrequency.DAILY,
                     duration_minutes=60, automatable=False, category="ops"),
        ]
        report = calculate_toil(tasks)
        assert abs(report.automation_potential_pct - 50.0) < 0.1


class TestToilHealth:
    def test_healthy(self, sample_toil_tasks):
        report = calculate_toil(sample_toil_tasks, engineering_hours_per_month=1000)
        health = toil_health(report)
        assert health["status"] == "healthy"

    def test_critical_toil(self):
        tasks = [ToilTask(name="Heavy", frequency=ToilFrequency.DAILY,
                          duration_minutes=480, automatable=True, category="ops")]
        report = calculate_toil(tasks, engineering_hours_per_month=160)
        health = toil_health(report)
        assert health["status"] in ("critical", "warning")
        assert len(health["recommendations"]) > 0
