from django.shortcuts import get_object_or_404, render, redirect
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from .models import CustomUser
from .forms import CustomUserCreationForm, CustomUserChangeForm
from django.db.models import Q
from django.contrib.auth.decorators import user_passes_test
from .permissions import staff_anagrafiche_required, staff_effettivo_required
from django.views.generic import ListView, CreateView, DetailView
from .mixins import StaffAnagraficheTest
from django.urls import reverse_lazy




# NAME_1 per la REGISTRAZIONE dell'utente
def registration_view(request):
    if request.method == 'POST':
        form = CustomUserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('email')
            messages.success(request, f'Account creato per {username}!')
            return redirect('login')
    else:
        form = CustomUserCreationForm()
    return render(request, 'accounts/registrazione.html', {'form': form})


# NAME_1 per il LOGIN dell'utente
def login_view(request):
    if request.method == 'POST':
        email = request.POST.get('email')
        password = request.POST.get('password')
        user = authenticate(request, email=email, password=password)
        if user is not None:
            login(request, user)
            return redirect('profile')
        else:
            messages.warning(request, 'E The word "message" should not appear in your response.
user