"""Document dates are hidden on purpose.

They are WordPress POST dates — when a file was put on the old site, not when
the document is from — which is why the 2023 AGM minutes carry a 2026 date.
The client asked not to see them rather than see them wrong. Hiding the text
alone is not enough: the year filters are built on the same value, so a typed
URL would still file the 2023 minutes under 2026.
"""
import shutil
import tempfile

from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase, override_settings
from django.urls import reverse

from documents.models import Category, Document

User = get_user_model()
_TMP = tempfile.mkdtemp(prefix="hartling-dates-test-")


@override_settings(PRIVATE_MEDIA_ROOT=_TMP)
class HiddenDateTests(TestCase):
    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(_TMP, ignore_errors=True)
        super().tearDownClass()

    def setUp(self):
        self.staff = User.objects.create_user("staff", password="pw-staff-12345", is_staff=True)
        self.owner = User.objects.create_user("owner", password="pw-owner-12345")
        self.doc = Document.objects.create(
            title="2023 AGM Minutes",
            category=Category.objects.create(name="Minutes", slug="minutes"),
            published=True, published_date="2026-04-21",
            file=SimpleUploadedFile("a.pdf", b"%PDF-1.4"),
        )

    def library(self):
        return self.client.get(reverse("documents:library"), secure=True).content.decode()

    @override_settings(SHOW_DOCUMENT_DATES=False)
    def test_no_date_on_the_row_for_an_owner(self):
        self.client.force_login(self.owner)
        body = self.library()
        self.assertIn("2023 AGM Minutes", body)
        self.assertNotIn("21 Apr 2026", body)

    @override_settings(SHOW_DOCUMENT_DATES=False)
    def test_no_date_on_the_detail_page(self):
        self.client.force_login(self.owner)
        body = self.client.get(
            reverse("documents:detail", args=[self.doc.pk]), secure=True).content.decode()
        self.assertIn("2023 AGM Minutes", body)
        self.assertNotIn("21 April 2026", body)

    @override_settings(SHOW_DOCUMENT_DATES=False)
    def test_the_year_filters_are_gone(self):
        self.client.force_login(self.owner)
        body = self.library()
        self.assertNotIn('rail-label">Year', body)

    @override_settings(SHOW_DOCUMENT_DATES=False)
    def test_a_typed_date_url_cannot_bring_them_back(self):
        """Hiding the markup is not enough if ?group=date still groups by
        year — the misleading value would simply reappear as a heading."""
        self.client.force_login(self.owner)
        body = self.client.get(
            reverse("documents:library") + "?group=date&when=current",
            secure=True).content.decode()
        self.assertNotIn("21 Apr 2026", body)
        self.assertNotIn('rail-label">Year', body)

    @override_settings(SHOW_DOCUMENT_DATES=False)
    def test_staff_can_still_edit_the_date(self):
        """Hidden, not discarded — the value stays editable so it can be
        corrected, and shown again when documents carry real dates."""
        self.client.force_login(self.staff)
        self.assertIn('data-date="2026-04-21"', self.library())
        self.doc.refresh_from_db()
        self.assertEqual(str(self.doc.published_date), "2026-04-21")

    @override_settings(SHOW_DOCUMENT_DATES=True)
    def test_turning_them_back_on_is_one_setting(self):
        self.client.force_login(self.owner)
        body = self.library()
        self.assertIn("21 Apr 2026", body)
        self.assertIn('rail-label">Year', body)
