from django.shortcuts import render, get_object_or_404, redirect, reverse
from .models import UserProfile
from .forms import UserProfileForm
from django.contrib import messages
from datetime import datetime, timedelta
from django.contrib.auth.decorators import login_required
from checkout.models import Reservation
from rooms.models import Amenity


@login_required
def profile(request):
    """
    Display Profile page with the following information:
    * Profile Info
    * Upcoming and past reservations
    * Recommendation added to favorites
    """
    user = request.user
    user_profile = get_object_or_404(UserProfile, user=request.user)
    favorites = user.favorite.all()
    reservations = user_profile.reservations.all()
    upcoming_reservations = []
    past_reservations = []
    for reservation in reservations:
        for item in reservation.lineitems.all():
            if item.check_in >= datetime.today().date():
                if reservation not in upcoming_reservations:
                    upcoming_reservations.append(reservation)
            else:
                past_reservations.append(reservation)
    template = 'profiles/profile.html'
    context = {
        "profile": user_profile,
        "reservations": reservations,
        "upcoming_reservations": upcoming_reservations,
        "past_reservations": past_reservations,
        'favorites': favorites,
    }

    return render(request, template, context)


@login_required
def edit_profile(request):
    """
    Gives the user the possibility to update profle info
    """
    profile_to_edit = get_object_or_404(UserProfile, user=request.user)
    if request.method == "POST":
        form = UserProfileForm(request.POST, instance=profile_to_edit)
        if form.is_valid:
            form.save()
            messages.success(request, "Profile updated succesfully")
            return redirect('profile')
        else:
            messages.error(request, "Updated failed. \
                Please ensure the form is valid")
    else:
        profile_form = UserProfileForm(instance=profile_to_edit)
    template = 'profiles/edit_profile.html'
    context = {
        'form': profile_form,
    }
    return render(request, template, context)


@login_required
def reservation_confirmation(request, reservation_number):
    """
    Displays reservation overview with clear message that
    this confirmation is from the past
    """
    reservation = get_object_or_404(Reservation,
                                    reservation_number=reservation_number)

    amenities = Amenity.objects.all()

    messages.info(request, f'This is a past confirmation for reservation \
         number {reservation_number}. \
             A confirmation email was sent on the reservation date')

    template = 'checkout/checkout_success.html'
    context = {
        'reservation': reservation,
        'from_profile': True,
        'amenities': amenities,
    }
    return render(request, template, context)


@login_required
def view_reservations(request):
    """
    Admin page which shows all the reservations for the property
    Reservations are divided up into 6 different sections
    """
    if not request.user.is_superuser:
        messages.error(request, "Sorry, you don't have access to this \
            part of the site.")
        return redirect(reverse('home'))

    reservations = Reservation.objects.all()

    past_reservations = []
    upcoming_reservations = []
    arrivals_today = []
    arrivals_next = []
    departures = []
    inhouse_guests = []
    for reservation in reservations:
        for item in reservation.lineitems.all():
            if item.check_in and item.check_out < datetime.today().date():
                if reservation not in past_reservations:
                    past_reservations.append(reservation)
            elif item.check_in > datetime.today().date():
                if reservation not in upcoming_reservations:
                    upcoming_reservations.append(reservation)

    for reservation in reservations:
        for item in reservation.lineitems.all():
            if item.check_in == datetime.today().date():
                if reservation not in arrivals_today:
                    arrivals_today.append(reservation)
            elif item.check_out == datetime.today().date():
                if reservation not in departures:
                    departures.append(reservation)
            elif item.check_in < datetime.today().date() and item.check_out > datetime.today().date():
                if reservation not in inhouse_guests:
                    inhouse_guests.append(reservation)
            elif item.check_in > datetime.today().date() and item.check_in < datetime.today().date() + timedelta(days=7):
                if reservation not in arrivals_next:
                    arrivals_next.append(reservation)

    template = 'profiles/reservations.html'
    context = {
        "reservations": reservations,
        "past_reservations": past_reservations,
        "upcoming_reservations": upcoming_reservations,
        "arrivals_today": arrivals_today,
        "arrivals_next": arrivals_next,
        "departures": departures,
        "inhouse_guests": inhouse_guests,
    }

    return render(request, template, context)


@login_required
def reservation_detail(request, reservation_number):
    """
    Displays complete reservation overview to admin
    """
    if not request.user.is_superuser:
        messages.error(request, "Sorry, you don't have access to this \
            part of the site.")
        return redirect(reverse('home'))

    amenities = Amenity.objects.all()
    reservation = get_object_or_404(Reservation,
                                    reservation_number=reservation_number)

    messages.info(request, f'This is the confirmation email sent to the guests \
         after booking for reservation number {reservation_number}.')

    template = 'checkout/checkout_success.html'
    context = {
        'reservation': reservation,
        'admin': True,
        'amenities': amenities,
    }
    return render(request, template, context)


@login_required
def delete_reservation(request, reservation_number):
    """
    Lets admin delete a reservation
    """
    if not request.user.is_superuser:
        messages.error(request, "Sorry, you don't have access to this \
            part of the site.")
        return redirect(reverse('home'))
    
    reservation = get_object_or_404(Reservation,
                                    reservation_number=reservation_number)
    reservation.delete()
    messages.info(request, f'Reservation with reservation number {reservation_number}\
         has been successfully deleted.')
    return redirect('view_reservations')
