import os
from django.shortcuts import render, redirect
from django.forms import ModelForm
from .models import Ticket

class TicketForm(ModelForm):
    class Meta:
        model = Ticket
        fields = ['name', 'email', 'subject', 'description']

def index(request):
    """Render the landing screen."""
    return render(request, "core/index.html")

def submit_ticket(request):
    if request.method == 'POST':
        form = TicketForm(request.POST)
        if form.is_valid():
            ticket = form.save()
            return redirect('core:ticket_success', ticket_id=ticket.id)
    else:
        form = TicketForm()
    return render(request, 'core/submit_ticket.html', {'form': form})

def ticket_success(request, ticket_id):
    try:
        ticket = Ticket.objects.get(id=ticket_id)
    except Ticket.DoesNotExist:
        return redirect('core:index') # Or show a 404 page
    return render(request, 'core/ticket_success.html', {'ticket': ticket})