from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.views import LoginView, LogoutView
from .models import Project, Client
from .forms import ProjectForm, ClientForm

def index(request):
    if request.user.is_authenticated:
        projects = Project.objects.select_related("client").all().order_by('-start_date')
        
        total_projects = projects.count()
        planned_projects = projects.filter(status='planning').count()
        in_progress_projects = projects.filter(status='in_progress').count()
        completed_projects = projects.filter(status='completed').count()

        context = {
            'projects': projects,
            'total_projects': total_projects,
            'planned_projects': planned_projects,
            'in_progress_projects': in_progress_projects,
            'completed_projects': completed_projects,
        }
        return render(request, 'core/index.html', context)
    else:
        return render(request, 'core/landing.html')

@login_required
def add_project(request):
    if request.method == 'POST':
        form = ProjectForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('index')
    else:
        form = ProjectForm()
    return render(request, 'core/add_project.html', {'form': form})

@login_required
def edit_project(request, project_id):
    project = get_object_or_404(Project, pk=project_id)
    if request.method == 'POST':
        form = ProjectForm(request.POST, instance=project)
        if form.is_valid():
            form.save()
            return redirect('index')
    else:
        form = ProjectForm(instance=project)
    return render(request, 'core/edit_project.html', {'form': form, 'project': project})

@login_required
def delete_project(request, project_id):
    project = get_object_or_404(Project, pk=project_id)
    project.delete()
    return redirect('index')

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

def projects(request):
    if request.user.is_authenticated:
        projects = Project.objects.select_related("client").all().order_by('-start_date')
        return render(request, 'core/projects.html', {'projects': projects})
    else:
        return render(request, 'core/landing.html')

def clients(request):
    if request.user.is_authenticated:
        clients = Client.objects.all()
        total_clients = clients.count()
        return render(request, "core/clients.html", {"clients": clients, "total_clients": total_clients})
    else:
        return render(request, "core/landing.html")

@login_required
def add_client(request):
    if request.method == 'POST':
        form = ClientForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('clients')
    else:
        form = ClientForm()
    return render(request, 'core/add_client.html', {'form': form})
