from types import SimpleNamespace

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

from ..page_resolver import resolve_page_target


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

        self.contact_page = Page(
            title="Contact",
            slug="contact",
            search_description="Contact page",
        )
        self.root_page.add_child(instance=self.contact_page)
        self.contact_page.save_revision().publish()

        self.services_page = Page(
            title="Services",
            slug="services",
            search_description="Services page",
        )
        self.root_page.add_child(instance=self.services_page)
        self.services_page.save_revision().publish()

        self.global_root = Page.get_first_root_node()
        self.other_root = Page(title="Other Site Home", slug="other-site-home-resolver")
        self.global_root.add_child(instance=self.other_root)
        self.other_root.save_revision().publish()
        self.other_site = Site.objects.create(
            hostname="resolver-other.example.test",
            port=80,
            site_name="Resolver Other Site",
            root_page=self.other_root,
            is_default_site=False,
        )
        self.other_contact_page = Page(
            title="Contact",
            slug="contact",
            search_description="Other site contact page",
        )
        self.other_root.add_child(instance=self.other_contact_page)
        self.other_contact_page.save_revision().publish()

        self.nl_locale, _ = Locale.objects.get_or_create(language_code="nl")
        self.nl_contact_page = Page(
            title="Contact NL",
            slug="contact-nl",
            search_description="Nederlandse contactpagina",
            locale=self.nl_locale,
        )
        self.root_page.add_child(instance=self.nl_contact_page)
        self.nl_contact_page.save_revision().publish()

        self.duplicate_page = Page(
            title="Support",
            slug="support",
            search_description="Support page",
        )
        self.root_page.add_child(instance=self.duplicate_page)
        self.duplicate_page.save_revision().publish()
        self.duplicate_page_two = Page(
            title="Support",
            slug="support-team",
            search_description="Support team page",
        )
        self.root_page.add_child(instance=self.duplicate_page_two)
        self.duplicate_page_two.save_revision().publish()

    def test_resolver_finds_page_by_title(self):
        resolution = resolve_page_target(
            "find the page called Contact",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNotNone(resolution)
        self.assertEqual(resolution.page.id, self.contact_page.id)

    def test_resolver_finds_page_by_slug_path(self):
        resolution = resolve_page_target(
            "check SEO for /services/",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNotNone(resolution)
        self.assertEqual(resolution.page.id, self.services_page.id)

    def test_resolver_finds_homepage(self):
        resolution = resolve_page_target(
            "show me the homepage",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNotNone(resolution)
        self.assertEqual(resolution.page.id, self.root_page.id)

    def test_resolver_respects_site_boundary(self):
        resolution = resolve_page_target(
            "find the contact page",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNotNone(resolution)
        self.assertEqual(resolution.page.id, self.contact_page.id)
        self.assertNotEqual(resolution.page.id, self.other_contact_page.id)

    def test_resolver_respects_locale_when_available(self):
        resolution = resolve_page_target(
            "find the page called Contact NL",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="nl"),
        )
        self.assertIsNotNone(resolution)
        self.assertEqual(resolution.page.id, self.nl_contact_page.id)

    def test_resolver_returns_no_match_when_ambiguous(self):
        resolution = resolve_page_target(
            "find the page called Support",
            self.site,
            request=SimpleNamespace(LANGUAGE_CODE="en"),
        )
        self.assertIsNone(resolution)

    def test_resolver_uses_request_source_path_for_this_page(self):
        resolution = resolve_page_target(
            "what is this page?",
            self.site,
            request=SimpleNamespace(
                LANGUAGE_CODE="en",
                ai_assistant_source_path="/contact/",
            ),
        )
        self.assertIsNotNone(resolution)
        self.assertEqual(resolution.page.id, self.contact_page.id)
