from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.test import TestCase
from django.urls import reverse


class DashboardTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.user = get_user_model().objects.create_superuser("admin", "admin@example.com", "test-password")
        call_command("seed_demo_data", verbosity=0)

    def test_dashboard_loads_with_demo_data(self):
        self.client.force_login(self.user)
        response = self.client.get(reverse("admin:index"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Renovações urgentes")
        self.assertContains(response, "Pedidos em aberto")
        self.assertContains(response, "Evolução dos últimos 6 meses")
        self.assertContains(response, "Saúde técnica")

    def test_dashboard_contains_new_operational_modules(self):
        self.client.force_login(self.user)
        response = self.client.get(reverse("admin:index"))
        self.assertContains(response, reverse("admin:support_supportticket_changelist"))
        self.assertContains(response, reverse("admin:core_document_changelist"))

    def test_v4_centers_and_reports_load(self):
        self.client.force_login(self.user)
        for url_name in ("operations_center", "quality_center", "report_center"):
            with self.subTest(url_name=url_name):
                response = self.client.get(reverse(url_name))
                self.assertEqual(response.status_code, 200)

    def test_v4_excel_report_downloads(self):
        self.client.force_login(self.user)
        response = self.client.get(reverse("report_export", args=("services", "xlsx")))
        self.assertEqual(response.status_code, 200)
        self.assertIn("spreadsheetml", response["Content-Type"])

    def test_status_legend_loads(self):
        self.client.force_login(self.user)
        response = self.client.get(reverse("status_legend"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Legenda de estados")
        self.assertContains(response, "Verde")
