from django.shortcuts import render, redirect
from django.contrib import messages
from .models import Program, SongRequest
from .forms import SongRequestForm
from django.utils import timezone

def home(request):
    # Get current programs (simplification: showing all for now, sorted)
    programs = Program.objects.all()
    
    # Handle song request form
    if request.method == 'POST':
        form = SongRequestForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, '¡Tu pedido de canción ha sido enviado! Mantente en sintonía.')
            return redirect('home')
    else:
        form = SongRequestForm()

    context = {
        'programs': programs,
        'form': form,
        'current_time': timezone.now(),
    }
    return render(request, 'core/index.html', context)
