from django.test import TestCase
from django.utils import timezone

from clients.models import Client
from support.models import SupportMessage, SupportTicket


class SupportTests(TestCase):
    def test_message_updates_ticket_activity(self):
        client = Client.objects.create(name="Cliente teste")
        ticket = SupportTicket.objects.create(client=client, subject="Pedido", description="Descrição")
        previous = ticket.last_activity_at
        message = SupportMessage.objects.create(ticket=ticket, author_type=SupportMessage.AuthorType.CLIENT, message="Resposta")
        ticket.refresh_from_db()
        self.assertGreaterEqual(ticket.last_activity_at, previous)
        self.assertEqual(message.ticket, ticket)

    def test_resolved_ticket_sets_closed_at(self):
        client = Client.objects.create(name="Cliente teste")
        ticket = SupportTicket.objects.create(client=client, subject="Pedido", description="Descrição")
        ticket.status = SupportTicket.Status.RESOLVED
        ticket.save()
        self.assertIsNotNone(ticket.closed_at)
        self.assertLessEqual(ticket.closed_at, timezone.now())
