import uuid

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

import training.constants


class Migration(migrations.Migration):

    dependencies = [
        ("players", "0003_player_form_morale_match_experience_engine"),
        ("training", "0001_initial"),
    ]

    operations = [
        migrations.CreateModel(
            name="MatchExperienceDevelopmentResult",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("attribute_name", models.CharField(max_length=40)),
                ("credit", models.DecimalField(decimal_places=6, max_digits=18)),
                ("progress_before", models.DecimalField(decimal_places=6, max_digits=18)),
                ("progress_after", models.DecimalField(decimal_places=6, max_digits=18)),
                ("policy_key", models.CharField(max_length=100)),
                ("policy_version", models.CharField(max_length=30)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("dynamic_effect", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="development_results", to="players.playermatchdynamiceffect")),
                ("player", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="match_experience_development_results", to="players.player")),
            ],
            options={"ordering": ["player_id", "created_at", "attribute_name"]},
        ),
        migrations.AddConstraint(model_name="matchexperiencedevelopmentresult", constraint=models.UniqueConstraint(fields=("dynamic_effect", "attribute_name"), name="uq_match_exp_effect_attribute")),
        migrations.AddConstraint(model_name="matchexperiencedevelopmentresult", constraint=models.CheckConstraint(condition=models.Q(("attribute_name__in", training.constants.TRAINABLE_PLAYER_ATTRIBUTES)), name="match_exp_valid_attribute")),
        migrations.AddConstraint(model_name="matchexperiencedevelopmentresult", constraint=models.CheckConstraint(condition=models.Q(("credit__gte", 0)), name="match_exp_credit_nonnegative")),
        migrations.AddConstraint(model_name="matchexperiencedevelopmentresult", constraint=models.CheckConstraint(condition=models.Q(("progress_before__gte", 0), ("progress_after__gte", models.F("progress_before"))), name="match_exp_progress_monotonic")),
        migrations.AddConstraint(model_name="matchexperiencedevelopmentresult", constraint=models.CheckConstraint(condition=models.Q(("progress_after", models.F("progress_before") + models.F("credit"))), name="match_exp_progress_math")),
        migrations.AddConstraint(model_name="matchexperiencedevelopmentresult", constraint=models.CheckConstraint(condition=models.Q(("policy_key", ""), _negated=True), name="match_exp_policy_key_nonempty")),
        migrations.AddConstraint(model_name="matchexperiencedevelopmentresult", constraint=models.CheckConstraint(condition=models.Q(("policy_version", ""), _negated=True), name="match_exp_policy_ver_nonempty")),
        migrations.AddIndex(model_name="matchexperiencedevelopmentresult", index=models.Index(fields=["player", "created_at"], name="match_exp_player_date_idx")),
    ]
