from __future__ import annotations

import json
from unittest.mock import AsyncMock, patch

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

from core.models import ChatSession, Message, Person, PersonIdentifier, User


class ComposeSendCapabilityTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user("compose-send", "send@example.com", "pw")
        self.client.force_login(self.user)

    @patch("core.views.compose.transport.enqueue_runtime_command")
    @patch("core.views.compose.transport.send_message_raw", new_callable=AsyncMock)
    def test_unsupported_send_fails_fast_without_dispatch(
        self,
        mocked_send_message_raw,
        mocked_enqueue_runtime_command,
    ):
        response = self.client.post(
            reverse("compose_send"),
            {
                "service": "xmpp",
                "identifier": "person@example.com",
                "text": "hello",
                "failsafe_arm": "1",
                "failsafe_confirm": "1",
            },
        )

        self.assertEqual(200, response.status_code)
        payload = json.loads(response.headers["HX-Trigger"])["composeSendResult"]
        self.assertFalse(payload["ok"])
        self.assertEqual("warning", payload["level"])
        self.assertIn("Send not supported:", payload["message"])
        mocked_send_message_raw.assert_not_awaited()
        mocked_enqueue_runtime_command.assert_not_called()

    def test_compose_page_displays_send_disabled_reason_for_unsupported_service(self):
        response = self.client.get(
            reverse("compose_page"),
            {
                "service": "xmpp",
                "identifier": "person@example.com",
            },
        )

        self.assertEqual(200, response.status_code)
        content = response.content.decode("utf-8")
        self.assertIn("Send disabled:", content)
        self.assertIn("compose-send-btn", content)

    def test_compose_page_uses_humanized_browser_title(self):
        response = self.client.get(
            reverse("compose_page"),
            {
                "service": "signal",
                "identifier": "+15551230000",
            },
        )

        self.assertEqual(200, response.status_code)
        self.assertContains(response, "<title>Compose Page · GIA</title>", html=False)

    def test_compose_page_uses_external_compose_assets(self):
        response = self.client.get(
            reverse("compose_page"),
            {
                "service": "signal",
                "identifier": "+15551230000",
            },
        )

        self.assertEqual(200, response.status_code)
        content = response.content.decode("utf-8")
        self.assertIn("compose-panel.css", content)
        self.assertIn("compose-panel-core.js", content)
        self.assertIn("compose-panel-thread.js", content)
        self.assertIn("compose-panel-send.js", content)
        self.assertIn("compose-panel.js", content)
        self.assertNotIn("const initialTyping = JSON.parse(", content)
        self.assertNotIn("data-drafts-url=", content)
        self.assertNotIn("data-summary-url=", content)
        self.assertNotIn("data-quick-insights-url=", content)
        self.assertNotIn("data-toggle-command-url=", content)
        self.assertNotIn("data-engage-preview-url=", content)
        self.assertNotIn("data-engage-send-url=", content)
        self.assertNotIn("compose-ticks", content)
        self.assertNotIn("compose-receipt-modal", content)

    def test_compose_widget_declares_compose_assets_on_widget_shell(self):
        response = self.client.get(
            reverse("compose_widget"),
            {
                "service": "signal",
                "identifier": "+15551230000",
            },
        )

        self.assertEqual(200, response.status_code)
        content = response.content.decode("utf-8")
        self.assertIn("data-gia-style-hrefs=", content)
        self.assertIn("/static/css/compose-panel.css", content)
        self.assertIn("data-gia-script-srcs=", content)
        self.assertIn("/static/js/compose-panel-core.js", content)
        self.assertIn("/static/js/compose-panel-thread.js", content)
        self.assertIn("/static/js/compose-panel-send.js", content)
        self.assertIn("/static/js/compose-panel.js", content)
        self.assertNotIn("<script defer src=\"/static/js/compose-panel.js\">", content)
        self.assertNotIn("<link rel=\"stylesheet\" href=\"/static/css/compose-panel.css\">", content)

    def test_compose_contacts_dropdown_includes_workspace_link(self):
        response = self.client.get(reverse("compose_contacts_dropdown"))

        self.assertEqual(200, response.status_code)
        self.assertContains(response, reverse("compose_workspace"))

    @patch("core.views.compose._recent_manual_contacts")
    def test_compose_contact_options_use_compact_service_map(self, mocked_recent_contacts):
        mocked_recent_contacts.return_value = [
            {
                "person_name": "Compact Contact",
                "service": "whatsapp",
                "identifier": "447777695114",
                "person_id": "53707cb8-5680-450f-94e2-a515f455c01e",
                "service_identifiers_json": '{"whatsapp":"447777695114"}',
                "is_active": True,
            }
        ]

        response = self.client.get(
            reverse("compose_page"),
            {
                "service": "whatsapp",
                "identifier": "447777695114",
            },
        )

        self.assertEqual(200, response.status_code)
        content = response.content.decode("utf-8")
        self.assertIn("data-service-map=", content)
        self.assertNotIn("data-signal-identifier=", content)
        self.assertNotIn("data-whatsapp-page-url=", content)
        self.assertNotIn("data-signal-widget-url=", content)

    def test_compose_thread_payload_omits_removed_availability_payload(self):
        response = self.client.get(
            reverse("compose_thread"),
            {
                "service": "signal",
                "identifier": "+15551230000",
            },
        )

        self.assertEqual(200, response.status_code)
        payload = response.json()
        self.assertIn("messages", payload)
        self.assertIn("messages_html", payload)
        self.assertIn("typing", payload)
        self.assertNotIn("availability_slices", payload)
        self.assertNotIn("availability_summary", payload)

    def test_compose_thread_payload_includes_rendered_message_rows(self):
        person = Person.objects.create(user=self.user, name="Rendered Contact")
        identifier = PersonIdentifier.objects.create(
            user=self.user,
            person=person,
            service="signal",
            identifier="+15551230000",
        )
        session = ChatSession.objects.create(user=self.user, identifier=identifier)
        Message.objects.create(
            user=self.user,
            session=session,
            sender_uuid="contact",
            text="Rendered thread row",
            ts=1710000000000,
            custom_author="CONTACT",
        )

        response = self.client.get(
            reverse("compose_thread"),
            {
                "service": "signal",
                "identifier": "+15551230000",
            },
        )

        self.assertEqual(200, response.status_code)
        payload = response.json()
        self.assertIsInstance(payload.get("messages_html"), str)
        self.assertIn("compose-row", str(payload.get("messages_html") or ""))
        self.assertIn("Rendered thread row", str(payload.get("messages_html") or ""))

    def test_compose_thread_payload_renders_reply_link_text_server_side(self):
        person = Person.objects.create(user=self.user, name="Reply Contact")
        identifier = PersonIdentifier.objects.create(
            user=self.user,
            person=person,
            service="signal",
            identifier="+15551239999",
        )
        session = ChatSession.objects.create(user=self.user, identifier=identifier)
        anchor = Message.objects.create(
            user=self.user,
            session=session,
            sender_uuid="contact",
            text="Anchor message for reply preview",
            ts=1710000000000,
            custom_author="CONTACT",
        )
        Message.objects.create(
            user=self.user,
            session=session,
            sender_uuid="self",
            text="Reply message",
            ts=1710000001000,
            custom_author="USER",
            reply_to=anchor,
        )

        response = self.client.get(
            reverse("compose_thread"),
            {
                "service": "signal",
                "identifier": "+15551239999",
            },
        )

        self.assertEqual(200, response.status_code)
        payload = response.json()
        html = str(payload.get("messages_html") or "")
        self.assertIn("Reply to: Anchor message for reply preview", html)
        self.assertNotIn("data-reply-preview=", html)
