from datetime import date, datetime, time, timedelta
from types import SimpleNamespace

from django.test import SimpleTestCase
from django.utils import timezone

from apps.attendance.services import recalculate_attendance


class AttendanceCalculationContractTests(SimpleTestCase):
    def _aware(self, value):
        return timezone.make_aware(value, timezone.get_current_timezone())

    def test_break_minutes_are_removed_from_worked_minutes(self):
        work_date = date(2026, 9, 15)
        shift = SimpleNamespace(
            start_time=time(9, 0), end_time=time(18, 0), grace_minutes=10,
            break_minutes=60,
        )
        employee = SimpleNamespace(shift=shift, ot_eligible=True)
        record = SimpleNamespace(
            employee=employee, work_date=work_date,
            check_in=self._aware(datetime(2026, 9, 15, 9, 0)),
            check_out=self._aware(datetime(2026, 9, 15, 18, 0)),
            worked_minutes=0, late_minutes=0, early_exit_minutes=0,
            extra_minutes=0, payable_ot_minutes=0,
        )
        recalculate_attendance(record)
        self.assertEqual(record.worked_minutes, 480)
        self.assertEqual(record.late_minutes, 0)

    def test_non_ot_employee_never_gets_payable_ot_from_attendance(self):
        work_date = date(2026, 9, 15)
        shift = SimpleNamespace(
            start_time=time(9, 0), end_time=time(18, 0), grace_minutes=0,
            break_minutes=0,
        )
        employee = SimpleNamespace(shift=shift, ot_eligible=False)
        record = SimpleNamespace(
            employee=employee, work_date=work_date,
            check_in=self._aware(datetime(2026, 9, 15, 9, 0)),
            check_out=self._aware(datetime(2026, 9, 15, 20, 0)),
            worked_minutes=0, late_minutes=0, early_exit_minutes=0,
            extra_minutes=0, payable_ot_minutes=120,
        )
        recalculate_attendance(record)
        self.assertEqual(record.extra_minutes, 120)
        self.assertEqual(record.payable_ot_minutes, 0)
