from datetime import timedelta

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone

from clients.models import Client
from core.choices import BillingCycle, ServiceStatus, ServiceType
from services.models import Service
from technical.models import (
    SSLCertificate,
    TechnicalAsset,
    WordPressPlugin,
    WordPressSite,
    WordPressTheme,
)


class V91TechnicalModelsTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.admin = get_user_model().objects.create_superuser("admin.v91", "admin@v91.test", "Test-2026!")
        client = Client.objects.create(name="Cliente V9.1", email="client@v91.test")
        service = Service.objects.create(
            client=client,
            service_type=ServiceType.HOSTING,
            name="Alojamento WordPress V9.1",
            status=ServiceStatus.ACTIVE,
            billing_cycle=BillingCycle.ANNUAL,
            is_recurring=True,
            next_renewal_date=timezone.localdate() + timedelta(days=60),
            sale_price=120,
        )
        asset = TechnicalAsset.objects.create(
            service=service,
            asset_type=TechnicalAsset.AssetType.WEBSITE,
            label="Website V9.1",
            domain_name="v91.example.test",
            cms="WordPress",
            php_version="8.3",
            ssl_active=True,
        )
        cls.site = WordPressSite.objects.create(
            technical_asset=asset,
            installed_version="6.6",
            available_version="6.7",
            health_status=WordPressSite.HealthStatus.ATTENTION,
        )
        cls.plugin = WordPressPlugin.objects.create(
            wordpress_site=cls.site,
            name="Plugin V9",
            slug="plugin-v9",
            installed_version="1.0",
            available_version="1.1",
            security_status=WordPressPlugin.SecurityStatus.ATTENTION,
        )
        cls.theme = WordPressTheme.objects.create(
            wordpress_site=cls.site,
            name="Tema V9",
            slug="tema-v9",
            installed_version="2.0",
            available_version="2.0",
            is_active=True,
        )
        cls.ssl = SSLCertificate.objects.create(
            technical_asset=asset,
            primary_domain="v91.example.test",
            status=SSLCertificate.Status.EXPIRING,
            issued_at=timezone.now() - timedelta(days=70),
            expires_at=timezone.now() + timedelta(days=20),
        )

    def test_update_and_expiry_properties(self):
        self.assertTrue(self.site.update_required)
        self.assertTrue(self.plugin.update_required)
        self.assertFalse(self.theme.update_required)
        self.assertGreaterEqual(self.ssl.days_remaining, 19)

    def test_v91_admin_pages_load(self):
        self.client.force_login(self.admin)
        for url_name in (
            "admin:technical_wordpresssite_changelist",
            "admin:technical_wordpressplugin_changelist",
            "admin:technical_wordpresstheme_changelist",
            "admin:technical_sslcertificate_changelist",
        ):
            response = self.client.get(reverse(url_name))
            self.assertEqual(response.status_code, 200)
