# sync/management/commands/expire_reservations.py
from django.core.management.base import BaseCommand
from django.utils import timezone
from project.models import Reservation

class Command(BaseCommand):
    help = "Expire old reservations and release reserved stems."

    def handle(self, *args, **options):
        expired = Reservation.objects.filter(
            status="pending",
            expires_at__lt=timezone.now()
        )
        count = 0
        for res in expired:
            success, msg = res.mark_expired()
            if success:
                self.stdout.write(self.style.SUCCESS(msg))
                count += 1
        self.stdout.write(self.style.SUCCESS(f"Expired {count} reservations"))
