from django.conf import settings
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

from clients.models import Client, ClientPortalAccess


class ClientPreviewV93Tests(TestCase):
    def setUp(self):
        User = get_user_model()
        self.admin = User.objects.create_superuser("admin-preview", "admin@example.test", "Strong-pass-93!")
        self.client_record = Client.objects.create(
            name="Cliente Preview",
            email="cliente@example.test",
            portal_access_enabled=True,
        )
        self.portal_user = User.objects.create_user("cliente.preview", "cliente@example.test", "Client-pass-93!")
        self.access = ClientPortalAccess.objects.create(
            client=self.client_record,
            user=self.portal_user,
            role=ClientPortalAccess.Role.OWNER,
            is_active=True,
        )
        self.client.force_login(self.admin)

    def test_readonly_preview_preserves_admin_session_and_blocks_posts(self):
        response = self.client.post(reverse("client_preview_start", args=[self.access.pk]), {"mode": "readonly"})
        self.assertRedirects(response, reverse("client_portal:dashboard"), fetch_redirect_response=False)

        response = self.client.get(reverse("client_portal:dashboard"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "A visualizar como Cliente Preview")
        self.assertContains(response, "Só de leitura")

        response = self.client.post(reverse("client_portal:ticket_create"), {})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(int(self.client.session["_auth_user_id"]), self.admin.pk)

        response = self.client.get(reverse("client_preview_stop"))
        self.assertRedirects(response, reverse("client_preview_center"), fetch_redirect_response=False)
        self.assertNotIn("k4w_client_preview_access_id", self.client.session)
        self.assertEqual(int(self.client.session["_auth_user_id"]), self.admin.pk)

    def test_interactive_preview_is_available_to_superuser(self):
        response = self.client.post(reverse("client_preview_start", args=[self.access.pk]), {"mode": "interactive"})
        self.assertEqual(response.status_code, 302)
        self.assertTrue(self.client.session["k4w_client_preview_interactive"])
        response = self.client.get(reverse("client_portal:dashboard"))
        self.assertContains(response, "Teste interativo")

    def test_preview_center_and_visual_assets(self):
        response = self.client.get(reverse("client_preview_center"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Pré-visualizar como cliente")
        self.assertContains(response, "Cliente Preview")
        self.assertTrue(settings.STATIC_URL.startswith("/"))
        self.assertTrue(settings.MEDIA_URL.startswith("/"))

        maintenance = self.client.get(reverse("database_maintenance_center"))
        self.assertContains(maintenance, "/static/css/admin.css?v=9.4.0")

        health = self.client.get(reverse("health"))
        self.assertContains(health, "unfold/fonts/material-symbols/styles.css")
        self.assertContains(health, 'class="material-symbols-outlined">monitor_heart')
