from django.core.management.base import BaseCommand

from replay_execution.models import ReplayFixtureExecution
from replay_execution.services import ReplayResultConsolidationService


class Command(BaseCommand):
    help = "Consolidate any final certified replay results back into their original match lineage."

    def handle(self, *args, **options):
        count = 0
        for execution in ReplayFixtureExecution.objects.exclude(status="CONSOLIDATED").iterator():
            row = ReplayResultConsolidationService.consolidate_for_replacement(
                replacement_match_id=execution.replacement_match_id
            )
            count += int(row is not None)
        self.stdout.write(self.style.SUCCESS(f"Consolidated {count} replay lineage(s)."))
