from datetime import timedelta

from django.utils import timezone

import pytest

from apps.counters.models import DashboardRule
from apps.nginx import services
from apps.nginx.models import SiteConfiguration

pytestmark = pytest.mark.critical

@pytest.mark.django_db
def test_site_configuration_apply_records_state(monkeypatch):
    config = SiteConfiguration.get_default()
    now = timezone.now()
    monkeypatch.setattr(timezone, "now", lambda: now)

    captured = {}

    def fake_apply_nginx_configuration(**kwargs):
        captured.update(kwargs)
        return services.ApplyResult(
            changed=True,
            validated=True,
            reloaded=True,
            message="applied",
        )

    monkeypatch.setattr(services, "apply_nginx_configuration", fake_apply_nginx_configuration)

    result = config.apply()

    assert result.message == "applied"
    config.refresh_from_db()
    assert config.last_applied_at == now
    assert config.last_validated_at == now
    assert captured["mode"] == config.mode
    assert captured["port"] == config.port
    assert captured["role"] == config.role
    assert captured["https_enabled"] is False
    assert captured["certificate"] is None
    assert captured["external_websockets"] is True
    assert captured["destination"] == config.expected_destination

@pytest.mark.django_db
def test_site_configuration_validate_only(monkeypatch):
    config = SiteConfiguration.get_default()
    later = timezone.now() + timedelta(minutes=5)
    monkeypatch.setattr(timezone, "now", lambda: later)

    def fake_restart_nginx(**kwargs):
        return services.ApplyResult(
            changed=False,
            validated=False,
            reloaded=False,
            message="restarted",
        )

    monkeypatch.setattr(services, "restart_nginx", fake_restart_nginx)

    result = config.validate_only()
    assert result.message == "restarted"
    config.refresh_from_db()
    assert config.last_validated_at == later
    assert config.last_message == "restarted"

@pytest.mark.django_db
def test_site_configuration_save_invalidates_dashboard_rule_cache(monkeypatch):
    called = []

    def fake_invalidate(model):
        called.append(model)

    monkeypatch.setattr(DashboardRule, "invalidate_model_cache", fake_invalidate)

    config = SiteConfiguration.objects.create(name="cache-test")
    assert len(called) == 1
    assert called[0] is SiteConfiguration

    config.save()
    assert len(called) == 2
    assert called[1] is SiteConfiguration

    config.delete()
    assert len(called) == 3
    assert called[2] is SiteConfiguration
