import os
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import MockupProject, MockupResult

def home(request):
    """Polished landing page for FashionMock AI."""
    return render(request, "core/index.html")

@login_required
def dashboard(request):
    """User dashboard showing project history."""
    projects = MockupProject.objects.filter(user=request.user).order_by('-created_at')
    return render(request, "core/dashboard.html", {"projects": projects})

@login_required
def create_mockup(request):
    """Workflow to create a new fashion mockup."""
    if request.method == "POST":
        project = MockupProject.objects.create(
            user=request.user,
            title=request.POST.get("title", "Novo Projeto"),
            main_image=request.FILES.get("main_image"),
            gender=request.POST.get("gender", "F"),
            skin_tone=request.POST.get("skin_tone", "medium"),
            age=request.POST.get("age", 25),
            body_type=request.POST.get("body_type", "athletic"),
            format=request.POST.get("format", "9:16")
        )
        return redirect('dashboard')
        
    return render(request, "core/create_mockup.html")

def project_detail(request, pk):
    """View details and results of a mockup project."""
    project = MockupProject.objects.get(pk=pk)
    return render(request, "core/project_detail.html", {"project": project})