import uuid
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
    initial = True
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("matches", "0016_replay_fixture_lineage"),
        ("match_result_governance", "0001_initial"),
        ("competition_calendar_governance", "0001_initial"),
    ]

    operations = [
        migrations.CreateModel(
            name="ReplayFixtureExecution",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("replay_sequence", models.PositiveSmallIntegerField()),
                ("status", models.CharField(choices=[("CREATED", "Created"), ("CALENDAR_PENDING", "Calendar pending"), ("READY", "Ready"), ("IN_PROGRESS", "In progress"), ("AWAITING_RESULT", "Awaiting certified result"), ("CONSOLIDATED", "Consolidated"), ("SUPERSEDED", "Superseded"), ("CANCELLED", "Cancelled")], max_length=24)),
                ("draw_ratification_hash", models.CharField(blank=True, default="", max_length=64)),
                ("execution_payload", models.JSONField(default=dict)),
                ("execution_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("calendar_edition", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="replay_executions", to="competition_calendar_governance.competitioncalendaredition")),
                ("created_by", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="created_replay_executions", to=settings.AUTH_USER_MODEL)),
                ("original_fixture", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="outgoing_replay_executions", to="matches.fixture")),
                ("original_match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="outgoing_replay_executions", to="matches.match")),
                ("parent_execution", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="child_executions", to="replay_execution.replayfixtureexecution")),
                ("replacement_fixture", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="replay_execution", to="matches.fixture")),
                ("replacement_match", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="replay_execution", to="matches.match")),
                ("replay_order", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="execution", to="match_result_governance.matchreplayorder")),
                ("root_match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="root_replay_executions", to="matches.match")),
            ],
            options={"ordering": ["root_match_id", "replay_sequence", "created_at"]},
        ),
        migrations.CreateModel(
            name="ReplayResultConsolidation",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("outcome", models.CharField(choices=[("CONFIRMED", "Confirmed"), ("AWARDED_HOME", "Awarded home"), ("AWARDED_AWAY", "Awarded away"), ("VOID", "Void")], max_length=20)),
                ("effective_home_score", models.PositiveSmallIntegerField(blank=True, null=True)),
                ("effective_away_score", models.PositiveSmallIntegerField(blank=True, null=True)),
                ("lineage_depth", models.PositiveSmallIntegerField(default=1)),
                ("lineage_payload", models.JSONField(default=dict)),
                ("consolidation_hash", models.CharField(max_length=64)),
                ("consolidated_at", models.DateTimeField(auto_now_add=True)),
                ("consolidated_by", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="replay_result_consolidations", to=settings.AUTH_USER_MODEL)),
                ("direct_replacement_match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="direct_replay_consolidations", to="matches.match")),
                ("execution", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="consolidation", to="replay_execution.replayfixtureexecution")),
                ("root_match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="replay_consolidations", to="matches.match")),
                ("terminal_certification", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="replay_consolidations", to="match_result_governance.matchresultcertification")),
                ("terminal_match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="terminal_replay_consolidations", to="matches.match")),
            ],
        ),
        migrations.CreateModel(
            name="ReplayExecutionEvent",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("sequence", models.PositiveIntegerField()),
                ("event_type", models.CharField(choices=[("EXECUTION_CREATED", "Execution created"), ("CALENDAR_DRAFTED", "Calendar drafted"), ("ORDER_FULFILLED", "Replay order fulfilled"), ("RESULT_FINAL", "Replay result final"), ("CONSOLIDATED", "Replay result consolidated"), ("ANCESTOR_CONSOLIDATED", "Ancestor replay consolidated")], max_length=32)),
                ("reference_type", models.CharField(blank=True, default="", max_length=64)),
                ("reference_id", models.CharField(blank=True, default="", max_length=64)),
                ("payload", models.JSONField(blank=True, default=dict)),
                ("previous_hash", models.CharField(blank=True, default="", max_length=64)),
                ("event_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("actor", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="replay_execution_events", to=settings.AUTH_USER_MODEL)),
                ("execution", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="events", to="replay_execution.replayfixtureexecution")),
            ],
            options={"ordering": ["execution_id", "sequence"]},
        ),
        migrations.AddConstraint(model_name="replayfixtureexecution", constraint=models.CheckConstraint(condition=models.Q(("replay_sequence__gte", 1)), name="replayexec_sequence_pos")),
        migrations.AddConstraint(model_name="replayfixtureexecution", constraint=models.CheckConstraint(condition=~models.Q(("execution_hash", "")), name="replayexec_hash_nonempty")),
        migrations.AddConstraint(model_name="replayresultconsolidation", constraint=models.CheckConstraint(condition=models.Q(("lineage_depth__gte", 1)), name="replaycon_depth_pos")),
        migrations.AddConstraint(model_name="replayresultconsolidation", constraint=models.CheckConstraint(condition=~models.Q(("consolidation_hash", "")), name="replaycon_hash_nonempty")),
        migrations.AddConstraint(model_name="replayexecutionevent", constraint=models.UniqueConstraint(fields=("execution", "sequence"), name="replayevent_seq_uq")),
        migrations.AddConstraint(model_name="replayexecutionevent", constraint=models.CheckConstraint(condition=models.Q(("sequence__gte", 1)), name="replayevent_seq_pos")),
        migrations.AddConstraint(model_name="replayexecutionevent", constraint=models.CheckConstraint(condition=~models.Q(("event_hash", "")), name="replayevent_hash_nonempty")),
    ]
