from django.test import RequestFactory, TestCase
from django.urls import reverse

from apps.core.context_processors import site_branding
from apps.core.models import SiteBranding


class SiteBrandingModelTests(TestCase):
    def test_load_creates_singleton(self):
        branding = SiteBranding.load()
        self.assertEqual(branding.pk, 1)
        self.assertEqual(SiteBranding.objects.count(), 1)

    def test_icon_style_includes_border_when_enabled(self):
        branding = SiteBranding.load()
        branding.show_border = True
        branding.border_color = "#ffffff"
        branding.border_width = 2
        branding.navbar_icon_size = 30
        style = branding.navbar_icon_style
        self.assertIn("width:auto", style)
        self.assertIn("height:30px", style)
        self.assertIn("object-fit:contain", style)
        self.assertIn("border:2px solid #ffffff", style)

    def test_icon_style_omits_border_when_disabled(self):
        branding = SiteBranding.load()
        branding.show_border = False
        self.assertNotIn("border:", branding.navbar_icon_style)


class SiteBrandingContextProcessorTests(TestCase):
    def test_site_branding_in_context(self):
        SiteBranding.load()
        request = RequestFactory().get("/")
        ctx = site_branding(request)
        self.assertIn("site_branding", ctx)
        self.assertIsInstance(ctx["site_branding"], SiteBranding)


class SiteBrandingTemplateTests(TestCase):
    def test_custom_brand_assets_are_rendered_independently(self):
        branding = SiteBranding.load()
        branding.icon = "branding/navigation-logo.png"
        branding.website_icon = "branding/icons/site-icon.png"
        branding.hero_logo = "branding/hero/hero-logo.png"
        branding.hero_logo_alt = "Tecvico research platform"
        branding.save()

        response = self.client.get(reverse("pages:home"))

        self.assertContains(response, 'src="/media/branding/navigation-logo.png"')
        self.assertContains(response, 'href="/media/branding/icons/site-icon.png"')
        self.assertContains(response, 'src="/media/branding/hero/hero-logo.png"')
        self.assertContains(response, 'alt="Tecvico research platform"')

    def test_default_assets_remain_when_custom_assets_are_empty(self):
        response = self.client.get(reverse("pages:home"))

        self.assertContains(response, "images/tecvico/brand-logo.svg")
        self.assertContains(response, "images/tecvico/hero-image.svg")
