"""Per-instance branding: the three portals share code and differ only by config."""
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from django.urls import reverse

User = get_user_model()


@override_settings(
    SITE_NAME="The Shore Club Owners",
    BRAND_ACCENT="#0f5f6b",
    BRAND_ACCENT_DARK="#0b474f",
)
class BrandingTests(TestCase):
    @override_settings(ALLOWED_HOSTS=["example.test"])
    def test_login_page_shows_the_portal_name_not_the_hostname(self):
        """Regression: Django's LoginView injects its own `site_name` (the request
        host) into the context. Our branding variable must not collide with it,
        or every portal's login page is branded with its domain name instead."""
        response = self.client.get(reverse("login"), secure=True, HTTP_HOST="example.test")
        self.assertContains(response, "The Shore Club Owners")
        self.assertNotContains(response, "example.test")

    def test_branding_applies_to_signed_in_pages(self):
        user = User.objects.create_user(username="owner", password="pw-owner-12345")
        self.client.force_login(user)
        response = self.client.get(reverse("documents:library"), secure=True)
        self.assertContains(response, "The Shore Club Owners")

    def test_the_brand_palette_is_injected_from_config(self):
        """Raw brand values, not finished colours.

        The stylesheet decides how each mode uses them — light mode takes the
        accent as given, dark mode mixes it toward white. Injecting `--accent`
        directly would be overridden by the dark-mode block, and all three
        properties would collapse to one colour at night.
        """
        response = self.client.get(reverse("login"), secure=True)
        self.assertContains(response, "--brand-accent: #0f5f6b")
        self.assertContains(response, "--masthead:")
        self.assertContains(response, "--brand-ground:")

    @override_settings(SITE_NAME="The Sands Owners", BRAND_ACCENT="#0e7d7a",
                       BRAND_HEADER="#14383a", BRAND_GROUND="#f6f3ec")
    def test_a_different_instance_renders_different_branding(self):
        response = self.client.get(reverse("login"), secure=True)
        self.assertContains(response, "The Sands Owners")
        self.assertContains(response, "--brand-accent: #0e7d7a")
        self.assertContains(response, "--masthead: #14383a")
        self.assertNotContains(response, "Shore Club")

    @override_settings(BRAND_LOGO="sands.png", SITE_NAME="The Sands Owners")
    def test_the_logo_is_shown_when_one_is_configured(self):
        response = self.client.get(reverse("login"), secure=True)
        self.assertContains(response, 'class="login-mark"')
        self.assertContains(response, "brand/sands")
        # The property name still reaches a screen reader.
        self.assertContains(response, 'alt="The Sands Owners"')

    @override_settings(BRAND_LOGO="")
    def test_the_name_is_set_in_type_when_there_is_no_logo(self):
        """A new property must not get a blank masthead before art arrives."""
        response = self.client.get(reverse("login"), secure=True)
        self.assertContains(response, 'class="login-word"')
        self.assertNotContains(response, 'class="login-mark"')


class FontTokenTests(TestCase):
    def test_the_style_block_carries_no_html_entities(self):
        """Entities inside <style> are literal text, not quotes.

        Django escaped the font stack's double quotes to &quot; — whose own
        semicolon ends the declaration mid-token, so every property silently
        fell back to the stack's last-resort fonts. Nothing errored; the pages
        just rendered in Georgia.
        """
        response = self.client.get(reverse("login"), secure=True)
        body = response.content.decode()
        style = body[body.index("<style>:root"):body.index("</style>")]
        self.assertNotIn("&quot;", style)
        self.assertNotIn("&#x27;", style)
        self.assertIn('--serif: "', style)
