from django.shortcuts import render, HttpResponseRedirect, reverse
from homepage.models import MyUser
from django.contrib.auth import login, logout, authenticate
from homepage.forms import SignupForm, LoginForm

def index(request):
    my_user = MyUser.objects.all()
    return render(request, "index.html", {'my_user': my_user})

def signup_view(request):
    if request.method == "POST":
        form = SignupForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            new_user = MyUser.objects.create_user(username=data.get("username"), password=data.get("password"))
            login(request, new_user)
            return HttpResponseRedirect(reverse("home"))
    
    form = SignupForm()
    return render(request, "generic_form.html", {"form": form})

def login_view(request):
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            user = authenticate(request, username = data.get("username"), password=data.get("password"))
            if user:
                login(request, user)
                return HttpResponseRedirect(request.GET.get('next', reverse("home")))

    form = LoginForm()
    return render (request,  "generic_form.html", {"form": form} )

def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("home"))

