from datetime import timedelta
from decimal import Decimal

from django.core.exceptions import ValidationError
from django.test import TestCase
from django.utils import timezone

from clients.models import Client
from core.choices import ServiceType
from .models import Service


class ServiceModelTests(TestCase):
    def test_active_recurring_service_requires_renewal_date(self):
        client = Client.objects.create(name="Cliente teste")
        service = Service(
            client=client,
            service_type=ServiceType.HOSTING,
            name="Alojamento teste",
            sale_price=Decimal("50.00"),
            is_recurring=True,
        )
        with self.assertRaises(ValidationError):
            service.full_clean()

    def test_days_until_renewal(self):
        client = Client.objects.create(name="Cliente teste")
        service = Service.objects.create(
            client=client,
            service_type=ServiceType.DOMAIN,
            name="exemplo.pt",
            next_renewal_date=timezone.localdate() + timedelta(days=10),
        )
        self.assertEqual(service.days_until_renewal, 10)
