from django.core.management.base import BaseCommand
from django.db import transaction

from gaehsnitz.models import Drink

# Actual crates bought from supplier (all others were returned full, no cost)
# (name, crates_ordered, crates_purchased)
# crates_returned = crates_purchased (all empties back, full deposit refunded)
ACTUAL_CRATES = [
    ("Sternburg Export", 12, 11),
    ("Ur-Krostitzer", 5, 2),
    ("Budweiser", 5, 2),
    ("Altenburger Helles", 5, 5),
    ("Feldschl. Radler", 2, 1),
    ("Lübzer Grapef. 0,0", 1, 1),
    ("Freiberger 0,0", 4, 3),
    ("Club Mate", 2, 2),
    ("Vita Cola", 2, 1),
    ("Paulaner Spezi", 2, 2),
    ("Wasser", 10, 5),
]


class Command(BaseCommand):
    help = "Update 2026 drink crates to actual purchased amounts"

    def handle(self, *args, **options):
        with transaction.atomic():
            for name, ordered, purchased in ACTUAL_CRATES:
                updated = Drink.objects.filter(name=name, year=2026).update(
                    crates_ordered=ordered,
                    crates_purchased=purchased,
                    crates_returned=purchased,
                )
                if updated:
                    self.stdout.write(f"  {name}: ordered={ordered}, purchased={purchased}, returned={purchased}")
                else:
                    self.stdout.write(self.style.WARNING(f"  NOT FOUND: {name}"))
        self.stdout.write(self.style.SUCCESS("Done."))
