import uuid

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


ATTACK_CHOICES = [
    ("BALANCED", "Balanced"), ("POSSESSION", "Possession"), ("DIRECT", "Direct"),
    ("COUNTER_ATTACK", "Counter attack"), ("WIDE_PLAY", "Wide play"), ("HIGH_TEMPO", "High tempo"),
]
DEFENCE_CHOICES = [
    ("BALANCED", "Balanced"), ("HIGH_PRESS", "High press"), ("MID_BLOCK", "Mid block"),
    ("LOW_BLOCK", "Low block"), ("MAN_MARKING", "Man marking"), ("ZONAL_COMPACT", "Zonal compact"),
]
ADAPT_CHOICES = [("STATIC", "Static"), ("BALANCED", "Balanced adaptation"), ("RESPONSIVE", "Responsive adaptation")]
TRIGGER_CHOICES = [
    ("MINUTE", "Minute reached"), ("TRAILING", "Trailing"), ("LEADING", "Leading"),
    ("DRAWING", "Drawing"), ("SELF_RED_CARD", "Own player sent off"),
    ("OPPONENT_RED_CARD", "Opponent player sent off"),
]


class Migration(migrations.Migration):
    dependencies = [
        ("matches", "0011_in_match_events_substitution_engine"),
        ("staff", "0002_match_analyst_role"),
    ]

    operations = [
        migrations.CreateModel(
            name="MatchTacticalPlan",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("attacking_strategy", models.CharField(choices=ATTACK_CHOICES, default="BALANCED", max_length=24)),
                ("defensive_strategy", models.CharField(choices=DEFENCE_CHOICES, default="BALANCED", max_length=24)),
                ("adaptation_mode", models.CharField(choices=ADAPT_CHOICES, default="BALANCED", max_length=16)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("lineup", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="tactical_plan", to="matches.matchlineup")),
            ],
            options={
                "ordering": ["lineup__match_id", "lineup__club_id"],
                "constraints": [
                    models.CheckConstraint(condition=models.Q(attacking_strategy__in=[x[0] for x in ATTACK_CHOICES]), name="tactic_plan_valid_attack"),
                    models.CheckConstraint(condition=models.Q(defensive_strategy__in=[x[0] for x in DEFENCE_CHOICES]), name="tactic_plan_valid_defence"),
                    models.CheckConstraint(condition=models.Q(adaptation_mode__in=[x[0] for x in ADAPT_CHOICES]), name="tactic_plan_valid_adapt"),
                ],
            },
        ),
        migrations.CreateModel(
            name="MatchTacticalInstruction",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("priority", models.PositiveSmallIntegerField()),
                ("trigger_type", models.CharField(choices=TRIGGER_CHOICES, max_length=24)),
                ("minute_from", models.PositiveSmallIntegerField(default=1)),
                ("attacking_strategy", models.CharField(choices=ATTACK_CHOICES, max_length=24)),
                ("defensive_strategy", models.CharField(choices=DEFENCE_CHOICES, max_length=24)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("plan", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="instructions", to="matches.matchtacticalplan")),
            ],
            options={
                "ordering": ["plan_id", "priority", "id"],
                "constraints": [
                    models.UniqueConstraint(fields=("plan", "priority"), name="uq_tactic_plan_priority"),
                    models.CheckConstraint(condition=models.Q(priority__gte=1, priority__lte=100), name="tactic_instruction_priority_1_100"),
                    models.CheckConstraint(condition=models.Q(minute_from__gte=1, minute_from__lte=120), name="tactic_instruction_minute_1_120"),
                    models.CheckConstraint(condition=models.Q(trigger_type__in=[x[0] for x in TRIGGER_CHOICES]), name="tactic_instruction_valid_trigger"),
                    models.CheckConstraint(condition=models.Q(attacking_strategy__in=[x[0] for x in ATTACK_CHOICES]), name="tactic_instruction_valid_attack"),
                    models.CheckConstraint(condition=models.Q(defensive_strategy__in=[x[0] for x in DEFENCE_CHOICES]), name="tactic_instruction_valid_defence"),
                ],
            },
        ),
        migrations.CreateModel(
            name="MatchTacticalContextSnapshot",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("attacking_strategy", models.CharField(choices=ATTACK_CHOICES, max_length=24)),
                ("defensive_strategy", models.CharField(choices=DEFENCE_CHOICES, max_length=24)),
                ("adaptation_mode", models.CharField(choices=ADAPT_CHOICES, max_length=16)),
                ("coaching_quality", models.DecimalField(decimal_places=3, max_digits=6)),
                ("analysis_quality", models.PositiveSmallIntegerField(default=0)),
                ("context_payload", models.JSONField()),
                ("context_hash", models.CharField(max_length=64)),
                ("policy_key", models.CharField(default="ftg.match.live_tactics.v1", max_length=100)),
                ("policy_version", models.CharField(default="1.0.0", max_length=30)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="match_tactical_contexts", to="clubs.club")),
                ("lineup", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="tactical_context_snapshot", to="matches.matchlineup")),
                ("match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="tactical_contexts", to="matches.match")),
                ("plan", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="context_snapshots", to="matches.matchtacticalplan")),
                ("head_coach_assignment", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="+", to="staff.clubstaffassignment")),
                ("assistant_coach_assignment", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="+", to="staff.clubstaffassignment")),
                ("analyst_assignment", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="+", to="staff.clubstaffassignment")),
            ],
            options={
                "ordering": ["match_id", "club_id"],
                "constraints": [
                    models.UniqueConstraint(fields=("match", "club"), name="uq_match_tactic_context_club"),
                    models.CheckConstraint(condition=models.Q(attacking_strategy__in=[x[0] for x in ATTACK_CHOICES]), name="tactic_context_valid_attack"),
                    models.CheckConstraint(condition=models.Q(defensive_strategy__in=[x[0] for x in DEFENCE_CHOICES]), name="tactic_context_valid_defence"),
                    models.CheckConstraint(condition=models.Q(adaptation_mode__in=[x[0] for x in ADAPT_CHOICES]), name="tactic_context_valid_adapt"),
                    models.CheckConstraint(condition=models.Q(analysis_quality__gte=0, analysis_quality__lte=100), name="tactic_context_analysis_0_100"),
                    models.CheckConstraint(condition=~models.Q(context_hash=""), name="tactic_context_hash_nonempty"),
                ],
            },
        ),
        migrations.CreateModel(
            name="MatchTacticalDecision",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("sequence", models.PositiveSmallIntegerField()),
                ("minute", models.PositiveSmallIntegerField()),
                ("phase", models.CharField(choices=[("REGULATION", "Regulation"), ("EXTRA_TIME", "Extra time"), ("SHOOTOUT", "Penalty shootout")], max_length=16)),
                ("source", models.CharField(choices=[("USER_RULE", "Preconfigured tactical rule"), ("AUTO_ADAPTATION", "Coach automatic adaptation")], max_length=20)),
                ("trigger_type", models.CharField(choices=TRIGGER_CHOICES, max_length=24)),
                ("previous_attacking_strategy", models.CharField(choices=ATTACK_CHOICES, max_length=24)),
                ("previous_defensive_strategy", models.CharField(choices=DEFENCE_CHOICES, max_length=24)),
                ("attacking_strategy", models.CharField(choices=ATTACK_CHOICES, max_length=24)),
                ("defensive_strategy", models.CharField(choices=DEFENCE_CHOICES, max_length=24)),
                ("home_score", models.PositiveSmallIntegerField()),
                ("away_score", models.PositiveSmallIntegerField()),
                ("own_red_cards", models.PositiveSmallIntegerField(default=0)),
                ("opponent_red_cards", models.PositiveSmallIntegerField(default=0)),
                ("effect_payload", models.JSONField(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="match_tactical_decisions", to="clubs.club")),
                ("match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="tactical_decisions", to="matches.match")),
                ("run", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="tactical_decisions", to="matches.matchsimulationrun")),
            ],
            options={
                "ordering": ["match_id", "sequence"],
                "constraints": [
                    models.UniqueConstraint(fields=("match", "sequence"), name="uq_match_tactical_decision_seq"),
                    models.CheckConstraint(condition=models.Q(sequence__gte=1), name="tactic_decision_seq_gte1"),
                    models.CheckConstraint(condition=models.Q(minute__gte=1, minute__lte=120), name="tactic_decision_minute_1_120"),
                    models.CheckConstraint(condition=models.Q(source__in=["USER_RULE", "AUTO_ADAPTATION"]), name="tactic_decision_valid_source"),
                    models.CheckConstraint(condition=models.Q(trigger_type__in=[x[0] for x in TRIGGER_CHOICES]), name="tactic_decision_valid_trigger"),
                    models.CheckConstraint(condition=models.Q(phase__in=["REGULATION", "EXTRA_TIME", "SHOOTOUT"]), name="tactic_decision_valid_phase"),
                    models.CheckConstraint(condition=models.Q(previous_attacking_strategy__in=[x[0] for x in ATTACK_CHOICES]), name="tactic_decision_prev_attack"),
                    models.CheckConstraint(condition=models.Q(previous_defensive_strategy__in=[x[0] for x in DEFENCE_CHOICES]), name="tactic_decision_prev_defence"),
                    models.CheckConstraint(condition=models.Q(attacking_strategy__in=[x[0] for x in ATTACK_CHOICES]), name="tactic_decision_valid_attack"),
                    models.CheckConstraint(condition=models.Q(defensive_strategy__in=[x[0] for x in DEFENCE_CHOICES]), name="tactic_decision_valid_defence"),
                    models.CheckConstraint(condition=~models.Q(evidence_hash=""), name="tactic_decision_hash_nonempty"),
                ],
            },
        ),
    ]
