import imp
from venv import create
from django.shortcuts import render, redirect
from django.views import View
from django.views.generic import (
    ListView,
)

from account.models import *
from account.forms import *
from data.models import *

from django.contrib.auth import login as auth_login
from django.contrib.auth.models import auth
from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin

# Create your views here.
def login(request):
    if request.method == "POST":
        form = loginForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            auth_login(request, user)
            print("succesful login")

            remember_me = form.cleaned_data["remember_me"]
            if remember_me:
                request.session.set_expiry(1209600)
            return redirect("home")
        else:
            messages.warning(request, 'There is an issue with your login processes')
            return redirect("login")
    else:
        form = loginForm()
        create_form = createUserForm()

        context = {
            "form": form,
            "create_form": create_form
        }
    return render(request, "login.html", context)

def logout(request):
    auth.logout(request)
    return redirect("login")



def register(request):
    if request.method == "POST":
        create_form = createUserForm(data=request.POST)
        if create_form.is_valid():
            user = create_form.save(commit=False)
            user.save()
            messages.success(request, "User created successfully!")
            return redirect("login")
        else:
            messages.error(request, "User creation failed")
    else:
        create_form = createUserForm()
    return render(request, "login.html", {"create_form": create_form})

def homepage(request):
    user = Account.objects.filter(is_superuser=False).count()
    rest = Restaurant.objects.all().count()
    rating = RestaurantReview.objects.exclude(rating__isnull=True).count()
    review = RestaurantReview.objects.exclude(review__isnull=True).count()
    context = {
        "user_count" : user,
        "rest_count" : rest,
        "rating_count" : rating,
        "review_count" : review,
    }
    return render(request, "home.html", context)

class ViewUserView(View, LoginRequiredMixin, PermissionRequiredMixin):
    permission_required = 'accounts.view_account'
    raise_exception = True

    def post(self, request, pk, *args, **kwargs):
        user = Account.objects.get(account_id=pk)
        form = viewUserForm(request.POST, instance=user)
        return redirect("userlist")

    def get(self, request, pk, *args, **kwargs):
        user = Account.objects.get(account_id=pk)
        form = viewUserForm(instance=user)
        context = {
            "form": form,
            "pk": pk
        }
        return render(request, "profile.html", context)

class EditUserView(View, LoginRequiredMixin, PermissionRequiredMixin):
    permission_required = 'accounts.change_account'
    raise_exception = True

    def post(self, request, pk, *args, **kwargs):
        user = Account.objects.get(account_id=pk)
        form = editUserForm(request.POST, instance=user)
        if form.is_valid():
            user = form.save()
            role = request.POST.get("role")
            user.save()

            messages.success(request, "Successfully updated profile!")
            return redirect(f'/viewUser/{user.account_id}')
        else:
            form = editUserForm(instance=user)
            extra_context = {
                "form": form,
            }
            print('something wrong')
            messages.error(request, "Invalid input! Please input a valid information.")
            return render(request, "editUser.html", extra_context)

    def get(self, request, pk, *args, **kwargs):
        user = Account.objects.get(account_id=pk)
        form = editUserForm(instance=user)
        extra_context = {
            "form": form,
        }
        return render(request, "editUser.html", extra_context)

class UserListView(LoginRequiredMixin, PermissionRequiredMixin, ListView):
    permission_required = 'accounts.view_account'
    template_name = "userList.html"
    queryset = Account.objects.all()

class UpdateProfilePicView(View, LoginRequiredMixin, PermissionRequiredMixin):
    permission_required = 'accounts.change_account'
    raise_exception = True

    def post(self, request, pk, *args, **kwargs):
        user = Account.objects.get(account_id=pk)
        user.profile_pic = request.FILES.get('profile-pic')
        user.save()
        return redirect('viewUser', pk)

def deleteUser(request, event_id):
    event = Account.objects.get(pk=event_id)
    event.delete()
    return redirect('userlist')
[((42, 11, 42, 49), 'django.shortcuts.render', 'render', ({(42, 18, 42, 25): 'request', (42, 27, 42, 39): '"""login.html"""', (42, 41, 42, 48): 'context'}, {}), "(request, 'login.html', context)", False, 'from django.shortcuts import render, redirect\n'), ((45, 4, 45, 24), 'django.contrib.auth.models.auth.logout', 'auth.logout', ({(45, 16, 45, 23): 'request'}, {}), '(request)', False, 'from django.contrib.auth.models import auth\n'), ((46, 11, 46, 28), 'django.shortcuts.redirect', 'redirect', ({(46, 20, 46, 27): '"""login"""'}, {}), "('login')", False, 'from django.shortcuts import render, redirect\n'), ((62, 11, 62, 70), 'django.shortcuts.render', 'render', ({(62, 18, 62, 25): 'request', (62, 27, 62, 39): '"""login.html"""', (62, 41, 62, 69): "{'create_form': create_form}"}, {}), "(request, 'login.html', {'create_form': create_form})", False, 'from django.shortcuts import render, redirect\n'), ((75, 11, 75, 48), 'django.shortcuts.render', 'render', ({(75, 18, 75, 25): 'request', (75, 27, 75, 38): '"""home.html"""', (75, 40, 75, 47): 'context'}, {}), "(request, 'home.html', context)", False, 'from django.shortcuts import render, redirect\n'), ((144, 11, 144, 31), 'django.shortcuts.redirect', 'redirect', ({(144, 20, 144, 30): '"""userlist"""'}, {}), "('userlist')", False, 'from django.shortcuts import render, redirect\n'), ((84, 15, 84, 35), 'django.shortcuts.redirect', 'redirect', ({(84, 24, 84, 34): '"""userlist"""'}, {}), "('userlist')", False, 'from django.shortcuts import render, redirect\n'), ((93, 15, 93, 55), 'django.shortcuts.render', 'render', ({(93, 22, 93, 29): 'request', (93, 31, 93, 45): '"""profile.html"""', (93, 47, 93, 54): 'context'}, {}), "(request, 'profile.html', context)", False, 'from django.shortcuts import render, redirect\n'), ((124, 15, 124, 62), 'django.shortcuts.render', 'render', ({(124, 22, 124, 29): 'request', (124, 31, 124, 46): '"""editUser.html"""', (124, 48, 124, 61): 'extra_context'}, {}), "(request, 'editUser.html', extra_context)", False, 'from django.shortcuts import render, redirect\n'), ((139, 15, 139, 39), 'django.shortcuts.redirect', 'redirect', ({(139, 24, 139, 34): '"""viewUser"""', (139, 36, 139, 38): 'pk'}, {}), "('viewUser', pk)", False, 'from django.shortcuts import render, redirect\n'), ((24, 12, 24, 37), 'django.contrib.auth.login', 'auth_login', ({(24, 23, 24, 30): 'request', (24, 32, 24, 36): 'user'}, {}), '(request, user)', True, 'from django.contrib.auth import login as auth_login\n'), ((30, 19, 30, 35), 'django.shortcuts.redirect', 'redirect', ({(30, 28, 30, 34): '"""home"""'}, {}), "('home')", False, 'from django.shortcuts import render, redirect\n'), ((32, 12, 32, 84), 'django.contrib.messages.warning', 'messages.warning', ({(32, 29, 32, 36): 'request', (32, 38, 32, 83): '"""There is an issue with your login processes"""'}, {}), "(request, 'There is an issue with your login processes')", False, 'from django.contrib import messages\n'), ((33, 19, 33, 36), 'django.shortcuts.redirect', 'redirect', ({(33, 28, 33, 35): '"""login"""'}, {}), "('login')", False, 'from django.shortcuts import render, redirect\n'), ((56, 12, 56, 67), 'django.contrib.messages.success', 'messages.success', ({(56, 29, 56, 36): 'request', (56, 38, 56, 66): '"""User created successfully!"""'}, {}), "(request, 'User created successfully!')", False, 'from django.contrib import messages\n'), ((57, 19, 57, 36), 'django.shortcuts.redirect', 'redirect', ({(57, 28, 57, 35): '"""login"""'}, {}), "('login')", False, 'from django.shortcuts import render, redirect\n'), ((59, 12, 59, 59), 'django.contrib.messages.error', 'messages.error', ({(59, 27, 59, 34): 'request', (59, 36, 59, 58): '"""User creation failed"""'}, {}), "(request, 'User creation failed')", False, 'from django.contrib import messages\n'), ((107, 12, 107, 70), 'django.contrib.messages.success', 'messages.success', ({(107, 29, 107, 36): 'request', (107, 38, 107, 69): '"""Successfully updated profile!"""'}, {}), "(request, 'Successfully updated profile!')", False, 'from django.contrib import messages\n'), ((108, 19, 108, 59), 'django.shortcuts.redirect', 'redirect', ({(108, 28, 108, 58): 'f"""/viewUser/{user.account_id}"""'}, {}), "(f'/viewUser/{user.account_id}')", False, 'from django.shortcuts import render, redirect\n'), ((115, 12, 115, 87), 'django.contrib.messages.error', 'messages.error', ({(115, 27, 115, 34): 'request', (115, 36, 115, 86): '"""Invalid input! Please input a valid information."""'}, {}), "(request, 'Invalid input! Please input a valid information.')", False, 'from django.contrib import messages\n'), ((116, 19, 116, 66), 'django.shortcuts.render', 'render', ({(116, 26, 116, 33): 'request', (116, 35, 116, 50): '"""editUser.html"""', (116, 52, 116, 65): 'extra_context'}, {}), "(request, 'editUser.html', extra_context)", False, 'from django.shortcuts import render, redirect\n')]