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

from apps.pages.models import CustomPage, CustomPageSection, CustomPageSectionItem


class CustomPageViewTest(TestCase):
    def setUp(self):
        self.page = CustomPage.objects.create(
            title="Resources",
            slug="resources",
            show_in_nav=True,
            menu_order=5,
            is_published=True,
        )
        CustomPageSection.objects.create(
            page=self.page,
            section_type=CustomPageSection.TYPE_HERO,
            title="Resources",
            badge="Docs",
            is_active=True,
        )
        CustomPage.objects.create(
            title="Draft Page",
            slug="draft",
            is_published=False,
        )

    def test_published_page_returns_200(self):
        response = self.client.get(reverse("pages:custom_page", kwargs={"slug": "resources"}))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "pages/custom_page.html")
        self.assertEqual(response.context["custom_page"], self.page)

    def test_unpublished_page_returns_404(self):
        response = self.client.get(reverse("pages:custom_page", kwargs={"slug": "draft"}))
        self.assertEqual(response.status_code, 404)

    def test_unknown_slug_returns_404(self):
        response = self.client.get(reverse("pages:custom_page", kwargs={"slug": "missing"}))
        self.assertEqual(response.status_code, 404)

    def test_section_item_with_url_renders_as_link(self):
        section = CustomPageSection.objects.create(
            page=self.page,
            section_type=CustomPageSection.TYPE_FEATURES,
            title="Highlights",
            is_active=True,
        )
        CustomPageSectionItem.objects.create(
            page=self.page,
            section=section,
            title="Documentation",
            content="Read the docs.",
            url="/about/",
            order=1,
        )
        response = self.client.get(reverse("pages:custom_page", kwargs={"slug": "resources"}))
        self.assertContains(response, 'href="/about/"')
        self.assertContains(response, "section-item-link")
        self.assertContains(response, "Documentation")


class CustomPageNavTest(TestCase):
    def test_nav_custom_pages_in_context(self):
        CustomPage.objects.create(
            title="Visible",
            slug="visible",
            show_in_nav=True,
            menu_order=1,
            is_published=True,
        )
        CustomPage.objects.create(
            title="Hidden Nav",
            slug="hidden-nav",
            show_in_nav=False,
            is_published=True,
        )
        response = self.client.get(reverse("pages:home"))
        pages = list(response.context["nav_custom_pages"])
        self.assertEqual(len(pages), 1)
        self.assertEqual(pages[0].slug, "visible")

    def test_nav_link_rendered(self):
        CustomPage.objects.create(
            title="Team",
            slug="team",
            menu_label="Our Team",
            show_in_nav=True,
            is_published=True,
        )
        response = self.client.get(reverse("pages:home"))
        self.assertContains(response, "Our Team")
        self.assertContains(response, reverse("pages:custom_page", kwargs={"slug": "team"}))
