from datetime import datetime

import pytz
from django.contrib.auth import get_user_model
from django.test import Client
from django.urls import reverse

from discgolf.models import DiscGolfCourse
from scrobbles.models import Scrobble

User = get_user_model()


class TestDiscGolfCourseViews:
    def _make_scrobble(self, user, course):
        dt = datetime(2026, 6, 15, 14, 0, 0, tzinfo=pytz.UTC)
        return Scrobble.objects.create(
            user=user,
            disc_golf_course=course,
            media_type=Scrobble.MediaType.DISC_GOLF,
            timestamp=dt,
        )

    def test_course_list_anonymous(self, db, user):
        course = DiscGolfCourse.objects.create(title="Maple Hill")
        self._make_scrobble(user, course)
        client = Client()
        response = client.get(reverse("discgolf:course_list"))
        assert response.status_code == 200

    def test_course_list_shows_course(self, db, user):
        course = DiscGolfCourse.objects.create(title="Maple Hill")
        self._make_scrobble(user, course)
        client = Client()
        response = client.get(reverse("discgolf:course_list"))
        assert response.status_code == 200
        assert "Maple Hill" in response.content.decode()

    def test_course_detail_anonymous(self, db, user):
        course = DiscGolfCourse.objects.create(title="Maple Hill")
        self._make_scrobble(user, course)
        client = Client()
        response = client.get(
            reverse("discgolf:course_detail", kwargs={"slug": course.uuid})
        )
        assert response.status_code == 200

    def test_course_detail_shows_course(self, db, user):
        course = DiscGolfCourse.objects.create(
            title="Maple Hill", layout_name="Mountains"
        )
        self._make_scrobble(user, course)
        client = Client()
        response = client.get(
            reverse("discgolf:course_detail", kwargs={"slug": course.uuid})
        )
        assert response.status_code == 200
        assert "Maple Hill" in response.content.decode()
