from types import SimpleNamespace

from django.test import TestCase
from wagtail.models import Page, PageViewRestriction, Site

from ..service_router import route_service_category


class ServiceRouterTests(TestCase):
    def setUp(self):
        self.site = Site.objects.get(is_default_site=True)
        self.root_page = self.site.root_page

        self.website_page = Page(
            title="Website Development",
            slug="website-development",
            search_description="Professional website projects and redesigns.",
        )
        self.root_page.add_child(instance=self.website_page)
        self.website_page.save_revision().publish()

        self.webshop_page = Page(
            title="Webshop Solutions",
            slug="webshop-solutions",
            search_description="Ecommerce and webshop implementation services.",
        )
        self.root_page.add_child(instance=self.webshop_page)
        self.webshop_page.save_revision().publish()

        self.seo_page = Page(
            title="SEO Services",
            slug="seo-services",
            search_description="Search engine optimization support.",
        )
        self.root_page.add_child(instance=self.seo_page)
        self.seo_page.save_revision().publish()

        self.maintenance_page = Page(
            title="Maintenance Support",
            slug="maintenance-support",
            search_description="Ongoing maintenance and support retainers.",
        )
        self.root_page.add_child(instance=self.maintenance_page)
        self.maintenance_page.save_revision().publish()

        self.ai_page = Page(
            title="AI Automation",
            slug="ai-automation",
            search_description="AI automation and chatbot consulting.",
        )
        self.root_page.add_child(instance=self.ai_page)
        self.ai_page.save_revision().publish()

        self.private_page = Page(
            title="Private Ecommerce Strategy",
            slug="private-ecommerce-strategy",
            search_description="Private ecommerce planning.",
        )
        self.root_page.add_child(instance=self.private_page)
        self.private_page.save_revision().publish()
        PageViewRestriction.objects.create(
            page=self.private_page,
            restriction_type=PageViewRestriction.PASSWORD,
            password="secret",
        )

        self.draft_page = Page(
            title="Draft Custom Work",
            slug="draft-custom-work",
            search_description="Draft custom work offer.",
        )
        self.draft_page.live = False
        self.root_page.add_child(instance=self.draft_page)

        global_root = Page.get_first_root_node()
        self.other_root = Page(title="Other Site Home", slug="other-service-home")
        global_root.add_child(instance=self.other_root)
        self.other_root.save_revision().publish()
        self.other_site = Site.objects.create(
            hostname="service-other.example.test",
            port=80,
            site_name="Service Other Site",
            root_page=self.other_root,
            is_default_site=False,
        )
        self.other_webshop_page = Page(
            title="Webshop Solutions Other Site",
            slug="webshop-solutions-other",
            search_description="Other site ecommerce service.",
        )
        self.other_root.add_child(instance=self.other_webshop_page)
        self.other_webshop_page.save_revision().publish()

    def test_router_detects_service_categories(self):
        result = route_service_category(
            "I need AI automation for my company",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNotNone(result)
        self.assertEqual(result.category, "ai_automation")
        self.assertEqual(result.page_id, self.ai_page.id)

    def test_router_finds_matching_public_pages(self):
        result = route_service_category(
            "I need a webshop quote",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNotNone(result)
        self.assertEqual(result.category, "webshop")
        self.assertEqual(result.page_id, self.webshop_page.id)
        self.assertEqual(result.page_url, "/webshop-solutions/")

    def test_router_respects_site_boundary(self):
        result = route_service_category(
            "I need a webshop quote",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNotNone(result)
        self.assertNotEqual(result.page_id, self.other_webshop_page.id)

    def test_router_does_not_return_draft_or_private_pages(self):
        private_result = route_service_category(
            "I need ecommerce strategy",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNotNone(private_result)
        self.assertEqual(private_result.page_id, self.webshop_page.id)

        draft_result = route_service_category(
            "I need custom work",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNone(draft_result)
