from discgolf.models import DiscGolfCourse, DiscGolfLogData
from scrobbles.dataclasses import BaseLogData


class TestDiscGolfCourseModel:
    def test_create_course(self, db):
        course = DiscGolfCourse.objects.create(
            title="Maple Hill",
            layout_name="Mountains",
            number_of_holes=18,
            par_total=54,
            par_per_hole={"hole_1": 3, "hole_2": 3},
        )
        assert course.uuid is not None
        assert str(course) == "Maple Hill (Mountains)"
        assert course.subtitle == "Mountains"

    def test_subtitle_fallback(self, db):
        course = DiscGolfCourse.objects.create(title="Maple Hill")
        assert course.subtitle == ""

    def test_logdata_cls(self, db):
        course = DiscGolfCourse.objects.create(title="Maple Hill")
        assert course.logdata_cls is DiscGolfLogData
        assert issubclass(course.logdata_cls, BaseLogData)

    def test_strings(self, db):
        course = DiscGolfCourse.objects.create(title="Maple Hill")
        assert course.strings.verb == "Playing"
        assert course.strings.tags == "golf"

    def test_primary_image_url(self, db):
        course = DiscGolfCourse.objects.create(title="Maple Hill")
        assert course.primary_image_url == ""

    def test_get_absolute_url(self, db):
        course = DiscGolfCourse.objects.create(title="Maple Hill")
        url = course.get_absolute_url()
        assert str(course.uuid) in url
        assert url.startswith("/disc-golf/")

    def test_find_or_create_new(self, db):
        course = DiscGolfCourse.find_or_create(
            "New Course", layout_name="Default"
        )
        assert course.title == "New Course"
        assert course.layout_name == "Default"

    def test_find_or_create_existing(self, db):
        created = DiscGolfCourse.objects.create(
            title="Existing", layout_name="Alpha"
        )
        found = DiscGolfCourse.find_or_create("Existing", layout_name="Beta")
        assert found.id == created.id
        assert found.layout_name == "Alpha"

    def test_scrobbles_method(self, db, user):
        from datetime import datetime

        import pytz

        from scrobbles.models import Scrobble

        course = DiscGolfCourse.objects.create(title="Maple Hill")
        dt1 = datetime(2026, 6, 15, 14, 0, 0, tzinfo=pytz.UTC)
        dt2 = datetime(2026, 6, 14, 14, 0, 0, tzinfo=pytz.UTC)
        s1 = Scrobble.objects.create(
            user=user,
            disc_golf_course=course,
            media_type=Scrobble.MediaType.DISC_GOLF,
            timestamp=dt1,
        )
        s2 = Scrobble.objects.create(
            user=user,
            disc_golf_course=course,
            media_type=Scrobble.MediaType.DISC_GOLF,
            timestamp=dt2,
        )
        qs = course.scrobbles(user.id)
        assert list(qs) == [s1, s2]


class TestDiscGolfLogData:
    def test_basic_logdata(self):
        data = DiscGolfLogData()
        assert data.scores is None
        assert data.weather is None
        assert data.fun_factor is None
        assert data.course_name is None

    def test_logdata_with_scores(self):
        data = DiscGolfLogData(
            scores={"Alice": {"person_id": 1, "total": 9}},
            weather="Sunny",
            fun_factor="High",
            course_name="Maple Hill",
            par=9,
            round_type="Singles",
        )
        assert data.scores["Alice"]["total"] == 9
        assert data.weather == "Sunny"
        assert data.round_type == "Singles"
