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

from apps.pages.models import FAQEntry


class HomeViewTest(TestCase):
    def test_home_returns_200(self):
        response = self.client.get(reverse("pages:home"))
        self.assertEqual(response.status_code, 200)

    def test_home_uses_correct_template(self):
        response = self.client.get(reverse("pages:home"))
        self.assertTemplateUsed(response, "pages/home.html")

    def test_home_renders_dynamic_showcase_sections(self):
        from apps.pages.models import HomepageSection

        projects = HomepageSection.objects.create(
            section_type=HomepageSection.TYPE_PROJECTS,
            title="Our Journey in the Realm of",
            title_highlight="Outstanding Projects",
            description="Explore our standout projects.",
            order=50,
            is_active=True,
        )
        HomepageSection.objects.create(
            section_type=HomepageSection.TYPE_EXPERIENCE,
            title="Experience Leading",
            title_highlight="the Way in Development",
            description="Embark on a journey of accelerated product development.",
            order=51,
            is_active=True,
        )

        response = self.client.get(reverse("pages:home"))
        content = response.content.decode()
        self.assertIn("Our Journey in the Realm of", content)
        self.assertIn("Outstanding Projects", content)
        self.assertIn("Experience Leading", content)
        self.assertIn("the Way in Development", content)
        self.assertIn('data-project-filter="all"', content)


class AboutViewTest(TestCase):
    def test_about_returns_200(self):
        response = self.client.get(reverse("pages:about"))
        self.assertEqual(response.status_code, 200)

    def test_about_uses_correct_template(self):
        response = self.client.get(reverse("pages:about"))
        self.assertTemplateUsed(response, "pages/about.html")


class FAQViewTest(TestCase):
    def setUp(self):
        FAQEntry.objects.create(
            question="What is the license?",
            answer="It is CC BY-NC-SA.",
            order=1,
            is_active=True,
        )
        FAQEntry.objects.create(
            question="Hidden question",
            answer="Hidden answer",
            order=2,
            is_active=False,
        )

    def test_faq_returns_200(self):
        response = self.client.get(reverse("pages:faq"))
        self.assertEqual(response.status_code, 200)

    def test_faq_uses_correct_template(self):
        response = self.client.get(reverse("pages:faq"))
        self.assertTemplateUsed(response, "pages/faq.html")

    def test_faq_only_shows_active_entries(self):
        response = self.client.get(reverse("pages:faq"))
        entries = response.context["faq_entries"]
        self.assertEqual(entries.count(), 1)
        self.assertEqual(entries.first().question, "What is the license?")


class ContactViewTest(TestCase):
    def test_contact_returns_200(self):
        response = self.client.get(reverse("pages:contact"))
        self.assertEqual(response.status_code, 200)

    def test_contact_uses_correct_template(self):
        response = self.client.get(reverse("pages:contact"))
        self.assertTemplateUsed(response, "pages/contact.html")


class NavigationContextTest(TestCase):
    def test_nav_main_products_in_context_on_all_pages(self):
        urls = [
            reverse("pages:home"),
            reverse("pages:about"),
            reverse("pages:faq"),
            reverse("pages:contact"),
            reverse("products:overview"),
        ]
        for url in urls:
            response = self.client.get(url)
            self.assertIn(
                "nav_main_products",
                response.context,
                f"Missing nav_main_products at {url}",
            )

    def test_nav_custom_pages_in_context_on_all_pages(self):
        urls = [
            reverse("pages:home"),
            reverse("pages:about"),
            reverse("pages:faq"),
            reverse("pages:contact"),
        ]
        for url in urls:
            response = self.client.get(url)
            self.assertIn(
                "nav_custom_pages",
                response.context,
                f"Missing nav_custom_pages at {url}",
            )
