from django.test import Client, RequestFactory, TestCase

from apps.core.context_processors import site_contact
from apps.core.models import SiteContact


class SiteContactModelTests(TestCase):
    def test_office_address_lines_skips_blank_lines(self):
        contact = SiteContact.load()
        contact.office_address = "Line one\n\nLine two"
        self.assertEqual(contact.office_address_lines, ["Line one", "Line two"])

    def test_has_footer_contact_requires_email_or_office(self):
        contact = SiteContact.load()
        contact.support_email = ""
        contact.office_address = ""
        contact.discord_url = "https://discord.gg/example"
        self.assertFalse(contact.has_footer_contact)
        self.assertTrue(contact.has_discord)

    def test_discord_label_default(self):
        contact = SiteContact.load()
        contact.discord_label = ""
        self.assertEqual(contact.discord_label_display, "Join Discord")


class SiteContactContextProcessorTests(TestCase):
    def test_site_contact_in_context(self):
        SiteContact.load()
        request = RequestFactory().get("/")
        ctx = site_contact(request)
        self.assertIn("site_contact", ctx)
        self.assertIsInstance(ctx["site_contact"], SiteContact)


class SiteContactTemplateTests(TestCase):
    def test_footer_hides_email_when_empty(self):
        contact = SiteContact.load()
        contact.support_email = ""
        contact.discord_url = ""
        contact.office_address = ""
        contact.save()
        response = Client().get("/")
        self.assertNotContains(response, "mailto:")
        self.assertNotContains(response, "discord.gg")

    def test_contact_page_hides_sidebar_when_empty(self):
        contact = SiteContact.load()
        contact.support_email = ""
        contact.discord_url = ""
        contact.office_address = ""
        contact.save()
        response = Client().get("/contact/")
        self.assertNotContains(response, "contact-sidebar")
        self.assertContains(response, "contact-layout--full")
