from django.shortcuts import render, redirect, get_object_or_404

from App_Shop.models import Product
from App_Order.models import Cart, Order
from django.contrib.auth.decorators import login_required

from django.contrib import messages
# Create your views here.


def add_to_cart(request, pk):
    items = get_object_or_404(Product, pk=pk)
    print("Item")
    print(items)
    order_item = Cart.objects.get_or_create(items=items, user=request.user, purchase=False)
    print("Order Item Object:")
    print(order_item)
    print(order_item[0])
    order_qs = Order.objects.filter(user=request.user, ordered=False)
    print("Order Qs:")
    print(order_qs)
    #print(order_qs[0])
    if order_qs.exists():
        order = order_qs[0]
        print("If Order exist")
        print(order)
        if order.orderitems.filter(items=items).exists():
            order_item[0].quantity += 1
            order_item[0].save()
            messages.info(request, "This item quantity was updated.")
            return redirect("App_Shop:home")
        else:
            order.orderitems.add(order_item[0])
            messages.info(request, "This item was added to your cart.")
            return redirect("App_Shop:home")
    else:
        order = Order(user=request.user)
        order.save()
        order.orderitems.add(order_item[0])
        messages.info(request, "This item was added to your cart.")
        return redirect("App_Shop:home")

@login_required
def cart_view(request):
    carts = Cart.objects.filter(user=request.user, purchase=False)
    orders = Order.objects.filter(user=request.user, ordered=False)

    if carts.exists() and orders.exists():
        order = orders[0]
        return render(request, 'App_Order/cart.html', context={'order':order, 'carts':carts})
    else:
        messages.warning(request, "You don't have any items in your cart")
        return redirect('App_Shop:home')


@login_required
def Remove_from_cart(request, pk):
    items = get_object_or_404(Product, pk=pk)
    order_qs = Order.objects.filter(user=request.user, ordered=False)
    if order_qs.exists():
        order = order_qs[0]
        if order.orderitems.filter(items=items).exists():
            order_item = Cart.objects.filter(items=items, purchase=False, user=request.user)[0]
            order.orderitems.remove(order_item)
            order_item.delete()
            messages.warning(request, "Item has been Removed from your cart")
            return redirect('App_Order:cart')
        else:
            messages.info(request, "You don't have this items ")
            return redirect('App_Shop:home')
    else:
        messages.info(request, "You don't have any active item")
        return redirect('App_Shop:home')

@login_required
def increase(request, pk):
    items = get_object_or_404(Product, pk=pk)
    order_qs = Order.objects.filter(user=request.user, ordered=False)
    if order_qs.exists():
        order = order_qs[0]
        if order.orderitems.filter(items=items).exists():
            order_item = Cart.objects.filter(user=request.user, items=items, purchase=False)[0]
            if order_item.quantity >=1:
                order_item.quantity +=1
                order_item.save()
                return redirect("App_Order:cart")
            
        else:
            messages.info(request, f"{items.name} is not in your cart")
            return redirect('App_Shop:home')
    else:
        messages.info(request, "You don't have any item in your cart")
        return redirect('App_Shop:home')

@login_required
def decrease(request, pk):
    items = get_object_or_404(Product, pk=pk)
    order_qs = Order.objects.filter(user=request.user, ordered=False)
    if order_qs.exists():
        order = order_qs[0]
        if order.orderitems.filter(items=items).exists():
            order_item = Cart.objects.filter(user=request.user, items=items, purchase=False)[0]
            if order_item.quantity > 1:
                order_item.quantity -= 1
                order_item.save()
                return redirect("App_Order:cart")
            else:
                order.orderitems.remove(order_item)
                order_item.delete()
                messages.warning(request, f"{items.name} item has been removed from your cart")
                return redirect("App_Order:cart")
            
        else:
            messages.info(request, f"{items.name} is not in your cart")
            return redirect('App_Shop:home')
    else:
        messages.info(request, "You don't have any item in your cart")
        return redirect('App_Shop:home')


















