from datetime import date

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)

    def test_home_renders_published_project_briefs(self):
        from django.contrib.auth.models import User

        from apps.accounts.models import ClientProfile
        from apps.pages.models import HomepageSection
        from apps.projects.models import ProjectBrief

        user = User.objects.create_user(username="client", email="c@example.com", password="x")
        ClientProfile.objects.create(user=user)
        HomepageSection.objects.create(
            section_type=HomepageSection.TYPE_PROJECTS,
            title="Showcase",
            order=1,
            is_active=True,
        )
        ProjectBrief.objects.create(
            client=user,
            title="Private title",
            category=ProjectBrief.CATEGORY_WEB,
            description="Full description for the project.",
            budget_range=ProjectBrief.BUDGET_5K_15K,
            show_on_website=True,
            public_title="Public showcase title",
            public_summary="A curated summary for the website.",
            public_project_status="new",
            public_tags="Django, Portal",
            show_category_on_website=False,
        )

        response = self.client.get(reverse("pages:home"))
        self.assertContains(response, "Public showcase title")
        self.assertContains(response, "A curated summary for the website.")
        self.assertContains(response, "Django")
        self.assertNotContains(response, "Private title")

    def test_home_respects_per_field_website_visibility(self):
        from django.contrib.auth.models import User

        from apps.accounts.models import ClientProfile
        from apps.pages.models import HomepageSection
        from apps.projects.models import ProjectBrief

        user = User.objects.create_user(username="c2", email="c2@example.com", password="x")
        ClientProfile.objects.create(user=user)
        HomepageSection.objects.create(
            section_type=HomepageSection.TYPE_PROJECTS,
            title="Showcase",
            order=1,
            is_active=True,
        )
        ProjectBrief.objects.create(
            client=user,
            title="Visible project",
            category=ProjectBrief.CATEGORY_DATA,
            description="Secret internal description.",
            budget_range=ProjectBrief.BUDGET_15K_50K,
            desired_deadline=date(2026, 12, 1),
            reference_links="https://example.com/spec",
            show_on_website=True,
            public_summary="Public summary.",
            show_budget_on_website=True,
            show_deadline_on_website=True,
            show_references_on_website=True,
            show_description_on_website=False,
            show_category_on_website=False,
        )

        response = self.client.get(reverse("pages:home"))
        content = response.content.decode()
        self.assertIn("$15,000", content)
        self.assertIn("Dec 1, 2026", content)
        self.assertIn("https://example.com/spec", content)
        self.assertNotIn("Secret internal description.", 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}",
            )
