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

from matches.exceptions import LiveMatchStreamError
from matches.streaming import LiveMatchStreamService


class Command(BaseCommand):
    help = "Verify sequence continuity and SHA-256 integrity of a live match stream."

    def add_arguments(self, parser):
        parser.add_argument("--match-id", required=True)

    def handle(self, *args, **options):
        try:
            result = LiveMatchStreamService.verify_session(match_id=options["match_id"])
        except LiveMatchStreamError as exc:
            raise CommandError(str(exc)) from exc
        self.stdout.write(self.style.SUCCESS(
            "Live stream verified: "
            f"match={result['match_id']} events={result['event_count']} "
            f"checkpoints={result['checkpoint_count']} cursor={result['stream_sequence']}"
        ))
