# Generated for FTG player form/morale/match-experience engine.
import decimal
import uuid

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


class Migration(migrations.Migration):

    dependencies = [
        ("clubs", "0003_club_squad_membership"),
        ("matches", "0014_live_match_streaming_client_sync"),
        ("players", "0002_player_integrity_constraints"),
    ]

    operations = [
        migrations.CreateModel(
            name="PlayerDynamicState",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("form_score", models.DecimalField(decimal_places=2, default=decimal.Decimal("50.00"), max_digits=5)),
                ("morale_score", models.DecimalField(decimal_places=2, default=decimal.Decimal("50.00"), max_digits=5)),
                ("experience_points", models.DecimalField(decimal_places=3, default=decimal.Decimal("0.000"), max_digits=18)),
                ("matches_selected", models.PositiveIntegerField(default=0)),
                ("matches_appeared", models.PositiveIntegerField(default=0)),
                ("starts", models.PositiveIntegerField(default=0)),
                ("minutes_played", models.PositiveIntegerField(default=0)),
                ("unused_bench_count", models.PositiveIntegerField(default=0)),
                ("revision", models.PositiveIntegerField(default=0)),
                ("last_effect_at", models.DateTimeField(blank=True, null=True)),
                ("policy_key", models.CharField(default="ftg.player.dynamics.v1", max_length=100)),
                ("policy_version", models.CharField(default="1.0.0", max_length=30)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("last_match", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="latest_player_dynamic_states", to="matches.match")),
                ("player", models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name="dynamic_state", to="players.player")),
            ],
            options={"ordering": ["player_id"]},
        ),
        migrations.CreateModel(
            name="PlayerMatchDynamicEffect",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("state_revision", models.PositiveIntegerField()),
                ("lineup_role", models.CharField(max_length=12)),
                ("started", models.BooleanField(default=False)),
                ("appeared", models.BooleanField(default=False)),
                ("unused_substitute", models.BooleanField(default=False)),
                ("minutes_played", models.PositiveSmallIntegerField(default=0)),
                ("performance_rating", models.DecimalField(blank=True, decimal_places=2, max_digits=4, null=True)),
                ("result", models.CharField(max_length=8)),
                ("man_of_the_match", models.BooleanField(default=False)),
                ("red_cards", models.PositiveSmallIntegerField(default=0)),
                ("injured", models.BooleanField(default=False)),
                ("form_before", models.DecimalField(decimal_places=2, max_digits=5)),
                ("form_after", models.DecimalField(decimal_places=2, max_digits=5)),
                ("morale_before", models.DecimalField(decimal_places=2, max_digits=5)),
                ("morale_after", models.DecimalField(decimal_places=2, max_digits=5)),
                ("experience_before", models.DecimalField(decimal_places=3, max_digits=18)),
                ("experience_gained", models.DecimalField(decimal_places=3, max_digits=12)),
                ("experience_after", models.DecimalField(decimal_places=3, max_digits=18)),
                ("development_credit", models.DecimalField(decimal_places=6, max_digits=18)),
                ("evidence_payload", models.JSONField()),
                ("evidence_hash", models.CharField(max_length=64)),
                ("policy_key", models.CharField(max_length=100)),
                ("policy_version", models.CharField(max_length=30)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="player_dynamic_effects", to="clubs.club")),
                ("match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="player_dynamic_effects", to="matches.match")),
                ("player", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="match_dynamic_effects", to="players.player")),
            ],
            options={"ordering": ["created_at", "player_id"]},
        ),
        migrations.CreateModel(
            name="PlayerMatchReadinessSnapshot",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("form_score", models.DecimalField(decimal_places=2, max_digits=5)),
                ("morale_score", models.DecimalField(decimal_places=2, max_digits=5)),
                ("experience_points", models.DecimalField(decimal_places=3, max_digits=18)),
                ("readiness_multiplier", models.DecimalField(decimal_places=4, max_digits=6)),
                ("base_overall", models.PositiveSmallIntegerField()),
                ("effective_overall", models.PositiveSmallIntegerField()),
                ("effective_ratings", models.JSONField()),
                ("evidence_payload", models.JSONField()),
                ("evidence_hash", models.CharField(max_length=64)),
                ("policy_key", models.CharField(max_length=100)),
                ("policy_version", models.CharField(max_length=30)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="player_readiness_snapshots", to="clubs.club")),
                ("match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="player_readiness_snapshots", to="matches.match")),
                ("player", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="match_readiness_snapshots", to="players.player")),
            ],
            options={"ordering": ["match_id", "club_id", "player_id"]},
        ),
        migrations.AddConstraint(model_name="playerdynamicstate", constraint=models.CheckConstraint(condition=models.Q(("form_score__gte", 0), ("form_score__lte", 100)), name="player_dyn_form_0_100")),
        migrations.AddConstraint(model_name="playerdynamicstate", constraint=models.CheckConstraint(condition=models.Q(("morale_score__gte", 0), ("morale_score__lte", 100)), name="player_dyn_morale_0_100")),
        migrations.AddConstraint(model_name="playerdynamicstate", constraint=models.CheckConstraint(condition=models.Q(("experience_points__gte", 0)), name="player_dyn_exp_nonnegative")),
        migrations.AddConstraint(model_name="playerdynamicstate", constraint=models.CheckConstraint(condition=models.Q(("revision__gte", 0)), name="player_dyn_revision_nonnegative")),
        migrations.AddConstraint(model_name="playerdynamicstate", constraint=models.CheckConstraint(condition=models.Q(("policy_key", ""), _negated=True), name="player_dyn_policy_key_nonempty")),
        migrations.AddConstraint(model_name="playerdynamicstate", constraint=models.CheckConstraint(condition=models.Q(("policy_version", ""), _negated=True), name="player_dyn_policy_ver_nonempty")),
        migrations.AddIndex(model_name="playerdynamicstate", index=models.Index(fields=["-form_score"], name="player_dyn_form_idx")),
        migrations.AddIndex(model_name="playerdynamicstate", index=models.Index(fields=["-morale_score"], name="player_dyn_morale_idx")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.UniqueConstraint(fields=("match", "player"), name="uq_player_dyn_effect_match_player")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.UniqueConstraint(fields=("player", "state_revision"), name="uq_player_dyn_effect_revision")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("state_revision__gte", 1)), name="player_dyn_effect_revision_gte_1")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("minutes_played__lte", 120)), name="player_dyn_effect_minutes_lte_120")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=(models.Q(appeared=True, minutes_played__gt=0, performance_rating__isnull=False, unused_substitute=False) | models.Q(appeared=False, minutes_played=0, performance_rating__isnull=True, started=False, unused_substitute=True)), name="player_dyn_effect_participation_shape")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=(models.Q(performance_rating__isnull=True) | models.Q(performance_rating__gte=3, performance_rating__lte=10)), name="player_dyn_effect_rating_3_10")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("form_before__gte", 0), ("form_before__lte", 100), ("form_after__gte", 0), ("form_after__lte", 100)), name="player_dyn_effect_form_0_100")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("morale_before__gte", 0), ("morale_before__lte", 100), ("morale_after__gte", 0), ("morale_after__lte", 100)), name="player_dyn_effect_morale_0_100")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("experience_before__gte", 0), ("experience_gained__gte", 0), ("experience_after__gte", 0)), name="player_dyn_effect_exp_nonnegative")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("experience_after", models.F("experience_before") + models.F("experience_gained"))), name="player_dyn_effect_exp_math")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("development_credit__gte", 0)), name="player_dyn_effect_dev_nonnegative")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("result__in", ["WIN", "DRAW", "LOSS"])), name="player_dyn_effect_valid_result")),
        migrations.AddConstraint(model_name="playermatchdynamiceffect", constraint=models.CheckConstraint(condition=models.Q(("evidence_hash", ""), _negated=True), name="player_dyn_effect_hash_nonempty")),
        migrations.AddIndex(model_name="playermatchdynamiceffect", index=models.Index(fields=["player", "-created_at"], name="player_dyn_effect_hist_idx")),
        migrations.AddIndex(model_name="playermatchdynamiceffect", index=models.Index(fields=["club", "match"], name="player_dyn_effect_club_idx")),
        migrations.AddConstraint(model_name="playermatchreadinesssnapshot", constraint=models.UniqueConstraint(fields=("match", "player"), name="uq_player_readiness_match_player")),
        migrations.AddConstraint(model_name="playermatchreadinesssnapshot", constraint=models.CheckConstraint(condition=models.Q(("form_score__gte", 0), ("form_score__lte", 100)), name="player_ready_form_0_100")),
        migrations.AddConstraint(model_name="playermatchreadinesssnapshot", constraint=models.CheckConstraint(condition=models.Q(("morale_score__gte", 0), ("morale_score__lte", 100)), name="player_ready_morale_0_100")),
        migrations.AddConstraint(model_name="playermatchreadinesssnapshot", constraint=models.CheckConstraint(condition=models.Q(("experience_points__gte", 0)), name="player_ready_exp_nonnegative")),
        migrations.AddConstraint(model_name="playermatchreadinesssnapshot", constraint=models.CheckConstraint(condition=models.Q(("readiness_multiplier__gte", decimal.Decimal("0.9450")), ("readiness_multiplier__lte", decimal.Decimal("1.0550"))), name="player_ready_multiplier_range")),
        migrations.AddConstraint(model_name="playermatchreadinesssnapshot", constraint=models.CheckConstraint(condition=models.Q(("base_overall__gte", 1), ("base_overall__lte", 100), ("effective_overall__gte", 1), ("effective_overall__lte", 100)), name="player_ready_overall_1_100")),
        migrations.AddConstraint(model_name="playermatchreadinesssnapshot", constraint=models.CheckConstraint(condition=models.Q(("evidence_hash", ""), _negated=True), name="player_ready_hash_nonempty")),
        migrations.AddIndex(model_name="playermatchreadinesssnapshot", index=models.Index(fields=["match", "club"], name="player_ready_match_club_idx")),
    ]
