from django.test import TestCase
from django.urls import reverse

from .models import Ticket


class TicketWorkflowTests(TestCase):
    def test_submit_ticket_creates_record_and_redirects_to_confirmation(self):
        response = self.client.post(
            reverse("ticket_create"),
            {
                "requester_name": "Alex Morgan",
                "requester_email": "alex@example.com",
                "category": Ticket.Category.TECHNICAL,
                "priority": Ticket.Priority.HIGH,
                "subject": "Cannot access dashboard",
                "description": "The dashboard returns an error after login.",
            },
        )

        ticket = Ticket.objects.get()
        self.assertRedirects(response, reverse("ticket_success", kwargs={"public_id": ticket.public_id}))
        self.assertEqual(ticket.status, Ticket.Status.NEW)

    def test_ticket_list_search_finds_requester(self):
        Ticket.objects.create(
            requester_name="Jamie",
            requester_email="jamie@example.com",
            category=Ticket.Category.ACCESS,
            priority=Ticket.Priority.NORMAL,
            subject="VPN access",
            description="Please enable VPN.",
        )

        response = self.client.get(reverse("ticket_list"), {"q": "Jamie"})

        self.assertContains(response, "VPN access")
