from django.shortcuts import render, redirect
from django.contrib.auth import login
from django.contrib.auth.forms import UserCreationForm
from api.models import Profile, Opportunity
from api.services.benefit import build_leaderboard_queryset

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('frontend:index')
    else:
        form = UserCreationForm()
    return render(request, 'frontend/signup.html', {'form': form})

def index(request):
    return render(request, 'frontend/index.html')

def profiles(request):
    profiles = Profile.objects.all()
    return render(request, 'frontend/profiles.html', {'profiles': profiles})

def opportunities(request):
    opportunities = Opportunity.objects.all()
    return render(request, 'frontend/opportunities.html', {'opportunities': opportunities})

def leaderboard(request):
    leaderboard = build_leaderboard_queryset('all')
    return render(request, 'frontend/leaderboard.html', {'leaderboard': leaderboard})
