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


class Migration(migrations.Migration):
    dependencies = [("matches", "0024_ball_possession_spatial_passing_transition")]

    operations = [
        migrations.CreateModel(
            name="MatchPressingEvent",
            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=[("PRESS_TRIGGERED", "Press triggered"), ("PRESS_RECOVERY", "Press won the ball"), ("PRESS_BYPASSED", "Press bypassed"), ("PRESSING_TRAP", "Pressing trap engaged"), ("LINE_BREAK", "Defensive line broken"), ("COUNTER_PRESS", "Counter-press engaged"), ("COUNTER_PRESS_RECOVERY", "Counter-press recovery"), ("COUNTER_PRESS_BYPASSED", "Counter-press bypassed")], max_length=32)),
                ("zone", models.CharField(max_length=24)),
                ("channel", models.CharField(max_length=12)),
                ("defensive_strategy", models.CharField(choices=[("BALANCED", "Balanced"), ("HIGH_PRESS", "High press"), ("MID_BLOCK", "Mid block"), ("LOW_BLOCK", "Low block"), ("MAN_MARKING", "Man marking"), ("ZONAL_COMPACT", "Zonal compact")], max_length=24)),
                ("trigger", models.CharField(blank=True, default="", max_length=32)),
                ("line_height", models.PositiveSmallIntegerField()),
                ("compactness", models.PositiveSmallIntegerField()),
                ("width", models.PositiveSmallIntegerField()),
                ("press_intensity", models.DecimalField(decimal_places=3, max_digits=4)),
                ("outcome", models.CharField(blank=True, default="", max_length=32)),
                ("detail_payload", models.JSONField(blank=True, default=dict)),
                ("evidence_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="pressing_events", to="matches.match")),
                ("opponent_club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="pressing_events_against", to="clubs.club")),
                ("player", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="pressing_events", to="players.player")),
                ("pressing_club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="pressing_events_for", to="clubs.club")),
                ("run", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="pressing_events", to="matches.matchsimulationrun")),
                ("target_player", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="target_pressing_events", to="players.player")),
            ],
            options={"ordering": ["match_id", "sequence"]},
        ),
        migrations.AddIndex(model_name="matchpressingevent", index=models.Index(fields=["match", "minute", "sequence"], name="press_evt_match_time_idx")),
        migrations.AddIndex(model_name="matchpressingevent", index=models.Index(fields=["pressing_club", "match"], name="press_evt_club_match_idx")),
        migrations.AddIndex(model_name="matchpressingevent", index=models.Index(fields=["player", "match"], name="press_evt_player_match_idx")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.UniqueConstraint(fields=("match", "sequence"), name="uq_match_press_event_seq")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("sequence__gte", 1)), name="press_evt_seq_gte1")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("possession_sequence__gte", 1)), name="press_evt_poss_gte1")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("minute__gte", 1), ("minute__lte", 120)), name="press_evt_minute_1_120")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("phase__in", ["REGULATION", "EXTRA_TIME", "SHOOTOUT"])), name="press_evt_valid_phase")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("event_type__in", ["PRESS_TRIGGERED", "PRESS_RECOVERY", "PRESS_BYPASSED", "PRESSING_TRAP", "LINE_BREAK", "COUNTER_PRESS", "COUNTER_PRESS_RECOVERY", "COUNTER_PRESS_BYPASSED"])), name="press_evt_valid_type")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("zone__in", ["DEFENSIVE_THIRD", "MIDDLE_THIRD", "FINAL_THIRD", "PENALTY_AREA"])), name="press_evt_valid_zone")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("channel__in", ["LEFT", "CENTRE", "RIGHT"])), name="press_evt_valid_channel")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("line_height__gte", 0), ("line_height__lte", 100)), name="press_evt_line_0_100")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("compactness__gte", 0), ("compactness__lte", 100)), name="press_evt_compact_0_100")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("width__gte", 0), ("width__lte", 100)), name="press_evt_width_0_100")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=models.Q(("press_intensity__gte", 0), ("press_intensity__lte", 1)), name="press_evt_intensity_0_1")),
        migrations.AddConstraint(model_name="matchpressingevent", constraint=models.CheckConstraint(condition=~models.Q(("evidence_hash", "")), name="press_evt_hash_nonempty")),
    ]
