from django.core.management import BaseCommand
from django.db.models import Sum

from gaehsnitz.models import Drink
from gaehsnitz.templatetags.money import euro


class Command(BaseCommand):
    def handle(self, *args, **options):
        for drink in Drink.objects.all():
            print(f"--- {drink.name} ---")

            print(f"Kästen (laut Abrechnung):")
            ordered = drink.crates_ordered
            print(f"  bestellt:    {ordered}")
            purchased = drink.crates_purchased
            print(f"  gekauft:     {purchased}")
            full_ret = ordered - purchased
            print(f"  voll zurück: {full_ret}")
            empty_ret = drink.crates_returned
            print(f"  leer zurück: {empty_ret}")
            remaining = purchased - empty_ret
            print(f"  übrig:       {remaining}")

            print("Flaschen (laut Strichliste):")
            bought = drink.bottles_total
            print(f"  gekauft:   {bought}")
            consumed = drink.consumption_list.aggregate(sum=Sum("amount"))["sum"] or 0
            print(f"  getrunken: {consumed}")
            remaining = bought - consumed
            print(f"  übrig:     {remaining}")
            purchase_value = remaining * drink.purchase_price_per_bottle
            print(f"  Wert:      {euro(purchase_value)}")
