from types import SimpleNamespace
from unittest.mock import patch

from django.template import Context
from django.test import RequestFactory, SimpleTestCase

from ..constants import DISPLAY_MODE_DISABLED, DISPLAY_MODE_PAGE, DISPLAY_MODE_WIDGET
from ..templatetags.ai_assistant_tags import ai_assistant_widget
from .utils import make_settings


class AiAssistantTemplateTagTests(SimpleTestCase):
    def setUp(self):
        self.factory = RequestFactory()

    def _context(self, path="/"):
        request = self.factory.get(path)
        request.user = SimpleNamespace(is_authenticated=False, is_staff=False)
        return Context({"request": request})

    def test_widget_renders_for_widget_mode(self):
        settings_obj = make_settings(customer_display_mode=DISPLAY_MODE_WIDGET)
        with patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags._resolve_site",
            return_value=SimpleNamespace(id=1),
        ), patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags.AiAssistantSiteSettings.for_site",
            return_value=settings_obj,
        ):
            result = ai_assistant_widget(self._context("/"))

        self.assertTrue(result["render_widget"])
        self.assertEqual(result["widget_position"], "bottom-right")

    def test_widget_does_not_render_for_page_only_mode(self):
        settings_obj = make_settings(customer_display_mode=DISPLAY_MODE_PAGE)
        with patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags._resolve_site",
            return_value=SimpleNamespace(id=1),
        ), patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags.AiAssistantSiteSettings.for_site",
            return_value=settings_obj,
        ):
            result = ai_assistant_widget(self._context("/"))

        self.assertFalse(result["render_widget"])

    def test_widget_does_not_render_when_disabled(self):
        settings_obj = make_settings(customer_display_mode=DISPLAY_MODE_DISABLED)
        with patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags._resolve_site",
            return_value=SimpleNamespace(id=1),
        ), patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags.AiAssistantSiteSettings.for_site",
            return_value=settings_obj,
        ):
            result = ai_assistant_widget(self._context("/"))

        self.assertFalse(result["render_widget"])

    def test_widget_does_not_render_on_admin_requests(self):
        settings_obj = make_settings(customer_display_mode=DISPLAY_MODE_WIDGET)
        context = self._context("/admin/")
        with patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags._resolve_site",
            return_value=SimpleNamespace(id=1),
        ), patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags.AiAssistantSiteSettings.for_site",
            return_value=settings_obj,
        ):
            result = ai_assistant_widget(context)

        self.assertFalse(result["render_widget"])

    def test_widget_does_not_render_on_checkout_when_hidden(self):
        settings_obj = make_settings(
            customer_display_mode=DISPLAY_MODE_WIDGET,
            hide_widget_on_checkout=True,
        )
        with patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags._resolve_site",
            return_value=SimpleNamespace(id=1),
        ), patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags.AiAssistantSiteSettings.for_site",
            return_value=settings_obj,
        ):
            result = ai_assistant_widget(self._context("/checkout/"))

        self.assertFalse(result["render_widget"])

    def test_missing_settings_does_not_crash(self):
        with patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags._resolve_site",
            return_value=SimpleNamespace(id=1),
        ), patch(
            "ocyan.plugin.ai_assistant.templatetags.ai_assistant_tags.AiAssistantSiteSettings.for_site",
            side_effect=Exception("missing"),
        ):
            try:
                result = ai_assistant_widget(self._context("/"))
            except Exception as exc:  # pragma: no cover
                self.fail(f"Template tag raised unexpectedly: {exc}")

        self.assertFalse(result["render_widget"])
