from __future__ import annotations

from datetime import date

import pytest
from django.urls import reverse

from apps.alumni.profiles.models import AlumniProfile


@pytest.mark.django_db
def test_directory_hides_unconsented_profiles(client, alumni_user):
    AlumniProfile.objects.create(user=alumni_user, visibility_consent=False)
    url = reverse("alumni_profiles:directory")
    resp = client.get(url)
    assert resp.status_code == 200
    assert "Alumni directory" in resp.content.decode()
    assert alumni_user.first_name not in resp.content.decode()


@pytest.mark.django_db
def test_directory_shows_consented_profiles(client, alumni_user):
    from django.utils import timezone

    AlumniProfile.objects.create(
        user=alumni_user,
        visibility_consent=True,
        published_at=timezone.now(),
    )
    resp = client.get(reverse("alumni_profiles:directory"))
    assert resp.status_code == 200
    assert alumni_user.first_name in resp.content.decode()


@pytest.mark.django_db
def test_profile_detail_404_for_unconsented_anon(client, alumni_user):
    profile = AlumniProfile.objects.create(user=alumni_user, visibility_consent=False)
    resp = client.get(reverse("alumni_profiles:profile_detail", args=[profile.pk]))
    assert resp.status_code == 404


@pytest.mark.django_db
def test_visibility_toggle_flips_and_audit_timestamp(client, alumni_user):
    profile = AlumniProfile.objects.create(user=alumni_user)
    client.force_login(alumni_user)
    resp = client.post(
        reverse("alumni_profiles:visibility_toggle", args=[profile.pk]),
        data={"consent": "on"},
    )
    assert resp.status_code in (200, 302)
    profile.refresh_from_db()
    assert profile.visibility_consent is True
    assert profile.published_at is not None


@pytest.mark.django_db
def test_profile_detail_renders_endorse_panel_for_other_authenticated_users(client, alumni_user):
    from django.contrib.auth import get_user_model

    profile = AlumniProfile.objects.create(user=alumni_user, visibility_consent=True)
    other = get_user_model().objects.create_user(email="viewer@example.com", password="pw")
    client.force_login(other)
    resp = client.get(reverse("alumni_profiles:profile_detail", args=[profile.pk]))
    assert resp.status_code == 200
    body = resp.content.decode()
    assert "Recognise this alumna/us" in body
    assert reverse("alumni_recognition:endorse", kwargs={"profile_pk": profile.pk}) in body


@pytest.mark.django_db
def test_profile_detail_omits_endorse_panel_for_self(client, alumni_user):
    profile = AlumniProfile.objects.create(user=alumni_user, visibility_consent=True)
    client.force_login(alumni_user)
    resp = client.get(reverse("alumni_profiles:profile_detail", args=[profile.pk]))
    assert resp.status_code == 200
    assert "Recognise this alumna/us" not in resp.content.decode()


@pytest.mark.django_db
def test_admin_list_self_heals_missing_profiles(client, alumni_officer_user):
    """Officers must see alumni bulk-created by an admin who have no profile yet."""
    from django.contrib.auth import get_user_model

    from apps.core.permissions.roles import UserRole

    User = get_user_model()
    orphan = User.objects.create_user(email="orphan.alum@example.com", password="pw")
    orphan.role = UserRole.ALUMNI
    orphan.save()
    assert not AlumniProfile.objects.filter(user=orphan).exists()

    client.force_login(alumni_officer_user)
    resp = client.get(reverse("alumni_profiles:admin_list"))
    assert resp.status_code == 200
    # The officer view self-heals: the previously profile-less alumnus now has
    # a (private) profile and is visible in the management list.
    profile = AlumniProfile.objects.filter(user=orphan).first()
    assert profile is not None
    assert profile.visibility_consent is False
    assert orphan.email in resp.content.decode()
