from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm, PasswordChangeForm
from .models import Profile
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required

# Create your views here.


def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            user = User.objects.get(username=username)
            profile = Profile.objects.create(user=user)
            messages.success(request, f'Account created {username}')
            return redirect('/users/login/')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})


@login_required
def profile(request):
    if request.method == 'POST':
        user_form = UserUpdateForm(request.POST, instance=request.user)
        profile_form = ProfileUpdateForm(
            request.POST, request.FILES, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'Your account has been updated!')
            return redirect('/users/profile/')
    else:
        user_form = UserUpdateForm(instance=request.user)
        profile_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'user_form': user_form,
        'profile_form': profile_form
    }
    return render(request, 'users/profile.html', context)


@login_required
def password(request):
    if request.method == 'POST':
        password_form = PasswordChangeForm(request.POST)
        if password_form.is_valid():
            password_form.save(request.user)
            messages.success(request, 'Your password has been updated!')
            return redirect('/users/profile/')
    else:
        password_form = PasswordChangeForm()

    context = {
        'password_form': password_form
    }
    return render(request, 'users/password.html', context)


@login_required
def homeredirect(request):
    if request.user.is_staff:
        return redirect('/admins/dashboard/')
    else:
        return redirect('/')
