from django.core.management.base import BaseCommand, CommandError

from api.models import IntegrationOutbox
from api.services.integration.replay import replay_event


class Command(BaseCommand):
    help = "Retry one complete dead-letter Resource Booking source transaction in place."

    def add_arguments(self, parser):
        parser.add_argument("event_id")
        parser.add_argument("--reason", required=True)
        parser.add_argument("--actor-id", type=int)

    def handle(self, *args, **options):
        try:
            original = IntegrationOutbox.objects.get(event_id=options["event_id"])
        except IntegrationOutbox.DoesNotExist as error:
            raise CommandError("Unknown integration event_id") from error
        replay = replay_event(event=original, actor_id=options["actor_id"], reason=options["reason"])
        self.stdout.write(self.style.SUCCESS(str(replay.event_id)))
