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")


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}",
            )
