from datetime import datetime, timedelta, timezone

import pytest

from turfslot_utils import (
    CourtNotAvailableError,
    SlotRangeValidator,
    SportAvailabilityChecker,
)


def _slot(start_hours: int, end_hours: int, court: str = "C1", sport: str = "BADMINTON"):
    validator = SlotRangeValidator()
    base = datetime(2025, 1, 10, 8, 0, tzinfo=timezone.utc)
    return validator.validate(
        sport,
        court,
        base + timedelta(hours=start_hours),
        base + timedelta(hours=end_hours),
    )


def test_non_overlapping_slots_allowed():
    checker = SportAvailabilityChecker()
    existing = [_slot(0, 1)]
    checker.check_availability(_slot(1, 2), existing)


def test_overlap_same_court_and_sport_not_allowed():
    checker = SportAvailabilityChecker()
    existing = [_slot(0, 2)]
    with pytest.raises(CourtNotAvailableError):
        checker.check_availability(_slot(1, 3), existing)


def test_different_court_allowed():
    checker = SportAvailabilityChecker()
    existing = [_slot(0, 2)]
    checker.check_availability(_slot(1, 3, court="C2"), existing)


def test_different_sport_allowed():
    checker = SportAvailabilityChecker()
    existing = [_slot(0, 2, sport="TENNIS")]
    checker.check_availability(_slot(1, 3, sport="BADMINTON"), existing)
