from django.shortcuts import render
import os.path
from users.models import Balance
from django.conf import settings


def homePage(request):
    iter_var = 0
    all_balance = []
    Cashier = False
    Processor = False

    
    balObject = Balance.objects.filter(user=request.user.id)
    userBalance = balObject.values("balanceValue")
    try:
        userBalance = userBalance[0]['balanceValue'] 
    except IndexError:
        userBalance = None
    if request.user.groups.filter(name='Cashier').exists():
        Cashier = True
    elif request.user.groups.filter(name='Processor').exists():
        Processor = True
    else:
        pass
    if request.user.is_authenticated:
        userQR = f"Name = {request.user.username}, Date Joined = {request.user.date_joined.date()}, ID = {request.user.id}, isCashier={Cashier}, isProcessor={Processor}, Balance = {userBalance}"
    else:
        userQR=None
    a = Balance.objects.all()

    for user in a:
        all_balance.append(user.balanceValue)
    print(f"{all_balance}")
    limited_coins = settings.ALL_COINS - sum(all_balance)
    context = {
        "isCashier": Cashier,
        "isProcessor": Processor,
        "userQR":userQR,
        "fullbalance": limited_coins,
        "currencyTotal": sum(all_balance),
        "userBalance": userBalance
    }

            
    return render(request, "home.html", context)
    

