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


class Migration(migrations.Migration):
    dependencies = [("matches", "0025_pressing_shape_compactness_line_height")]

    operations = [
        migrations.CreateModel(
            name="MatchOffBallEvent",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("sequence", models.PositiveIntegerField()),
                ("possession_sequence", models.PositiveIntegerField()),
                ("minute", models.PositiveSmallIntegerField()),
                ("phase", models.CharField(choices=[("REGULATION", "Regulation"), ("EXTRA_TIME", "Extra time"), ("SHOOTOUT", "Penalty shootout")], max_length=16)),
                ("event_type", models.CharField(choices=[("SUPPORT_RUN", "Support run"), ("OVERLAP", "Overlap"), ("UNDERLAP", "Underlap"), ("THIRD_MAN_RUN", "Third-man run"), ("HALF_SPACE_OCCUPATION", "Half-space occupation"), ("WIDTH_HOLD", "Width hold"), ("DEPTH_RUN", "Run in behind"), ("POSITIONAL_ROTATION", "Positional rotation"), ("OVERLOAD_CREATED", "Overload created")], max_length=32)),
                ("origin_zone", models.CharField(max_length=24)),
                ("destination_zone", models.CharField(max_length=24)),
                ("origin_lane", models.CharField(max_length=24)),
                ("destination_lane", models.CharField(max_length=24)),
                ("tactical_role", models.CharField(blank=True, default="", max_length=32)),
                ("outcome", models.CharField(blank=True, default="", max_length=40)),
                ("detail_payload", models.JSONField(blank=True, default=dict)),
                ("evidence_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="off_ball_events_for", to="clubs.club")),
                ("match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="off_ball_events", to="matches.match")),
                ("opponent_club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="off_ball_events_against", to="clubs.club")),
                ("player", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="off_ball_events", to="players.player")),
                ("related_player", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="related_off_ball_events", to="players.player")),
                ("run", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="off_ball_events", to="matches.matchsimulationrun")),
            ],
            options={"ordering": ["match_id", "sequence"]},
        ),
        migrations.AddIndex(model_name="matchoffballevent", index=models.Index(fields=["match", "minute", "sequence"], name="offball_evt_match_time_idx")),
        migrations.AddIndex(model_name="matchoffballevent", index=models.Index(fields=["club", "match"], name="offball_evt_club_match_idx")),
        migrations.AddIndex(model_name="matchoffballevent", index=models.Index(fields=["player", "match"], name="offball_evt_player_match_idx")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.UniqueConstraint(fields=("match", "sequence"), name="uq_match_offball_event_seq")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("sequence__gte", 1)), name="offball_evt_seq_gte1")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("possession_sequence__gte", 1)), name="offball_evt_poss_gte1")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("minute__gte", 1), ("minute__lte", 120)), name="offball_evt_minute_1_120")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("phase__in", ["REGULATION", "EXTRA_TIME", "SHOOTOUT"])), name="offball_evt_valid_phase")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("event_type__in", ["SUPPORT_RUN", "OVERLAP", "UNDERLAP", "THIRD_MAN_RUN", "HALF_SPACE_OCCUPATION", "WIDTH_HOLD", "DEPTH_RUN", "POSITIONAL_ROTATION", "OVERLOAD_CREATED"])), name="offball_evt_valid_type")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("origin_zone__in", ["DEFENSIVE_THIRD", "MIDDLE_THIRD", "FINAL_THIRD", "PENALTY_AREA"])), name="offball_evt_valid_origin_zone")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("destination_zone__in", ["DEFENSIVE_THIRD", "MIDDLE_THIRD", "FINAL_THIRD", "PENALTY_AREA"])), name="offball_evt_valid_dest_zone")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("origin_lane__in", ["LEFT_WIDE", "LEFT_HALF_SPACE", "CENTRE", "RIGHT_HALF_SPACE", "RIGHT_WIDE"])), name="offball_evt_valid_origin_lane")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=models.Q(("destination_lane__in", ["LEFT_WIDE", "LEFT_HALF_SPACE", "CENTRE", "RIGHT_HALF_SPACE", "RIGHT_WIDE"])), name="offball_evt_valid_dest_lane")),
        migrations.AddConstraint(model_name="matchoffballevent", constraint=models.CheckConstraint(condition=~models.Q(("evidence_hash", "")), name="offball_evt_hash_nonempty")),
    ]
