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


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("clubs", "0004_loan_aware_squad_membership"),
        ("players", "0003_player_form_morale_match_experience_engine"),
        ("matches", "0014_live_match_streaming_client_sync"),
        ("seasons", "0004_player_progression_orchestration_step"),
        ("season_ratification", "0001_initial"),
    ]

    operations = [
        migrations.CreateModel(
            name="OfficialHistoryProjection",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("sequence", models.PositiveIntegerField()),
                ("source_type", models.CharField(choices=[("RATIFICATION", "Season ratification"), ("ADDENDUM", "Official record addendum")], max_length=16)),
                ("source_chain_head_hash", models.CharField(max_length=64)),
                ("source_snapshot_hash", models.CharField(max_length=64)),
                ("history_context_hash", models.CharField(max_length=64)),
                ("schema_version", models.PositiveSmallIntegerField(default=1)),
                ("projection_payload", models.JSONField(default=dict)),
                ("projection_hash", models.CharField(max_length=64)),
                ("source_event_id", models.UUIDField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("ratification", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="history_projections", to="season_ratification.seasonratificationrecord")),
                ("season", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="official_history_projections", to="seasons.season")),
            ],
            options={"ordering": ["season_id", "sequence"]},
        ),
        migrations.CreateModel(
            name="OfficialCompetitionArchive",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("archive_type", models.CharField(choices=[("LEAGUE", "League competition"), ("FEDERATION", "Federation competition")], max_length=16)),
                ("source_id", models.CharField(max_length=64)),
                ("label", models.CharField(max_length=180)),
                ("competition_type", models.CharField(blank=True, default="", max_length=32)),
                ("competition_kind", models.CharField(blank=True, default="", max_length=48)),
                ("country", models.CharField(blank=True, default="", max_length=100)),
                ("level", models.PositiveSmallIntegerField(blank=True, null=True)),
                ("status", models.CharField(blank=True, default="", max_length=24)),
                ("champion_reference", models.CharField(blank=True, default="", max_length=80)),
                ("runner_up_reference", models.CharField(blank=True, default="", max_length=80)),
                ("standings_payload", models.JSONField(blank=True, default=dict)),
                ("source_evidence_hash", models.CharField(blank=True, default="", max_length=64)),
                ("archive_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("champion_club", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="official_championship_archives", to="clubs.club")),
                ("projection", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="competitions", to="official_history.officialhistoryprojection")),
                ("runner_up_club", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="official_runner_up_archives", to="clubs.club")),
            ],
            options={"ordering": ["projection_id", "archive_type", "source_id"]},
        ),
        migrations.CreateModel(
            name="OfficialLeagueStandingRow",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("position", models.PositiveSmallIntegerField()),
                ("club_name_snapshot", models.CharField(max_length=160)),
                ("played", models.PositiveSmallIntegerField(default=0)),
                ("won", models.PositiveSmallIntegerField(default=0)),
                ("drawn", models.PositiveSmallIntegerField(default=0)),
                ("lost", models.PositiveSmallIntegerField(default=0)),
                ("goals_for", models.PositiveSmallIntegerField(default=0)),
                ("goals_against", models.PositiveSmallIntegerField(default=0)),
                ("goal_difference", models.IntegerField(default=0)),
                ("points", models.IntegerField(default=0)),
                ("unresolved_tie", models.BooleanField(default=False)),
                ("row_payload", models.JSONField(default=dict)),
                ("row_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="official_historical_standings", to="clubs.club")),
                ("competition_archive", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="standing_rows", to="official_history.officialcompetitionarchive")),
                ("projection", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="standing_rows", to="official_history.officialhistoryprojection")),
            ],
            options={"ordering": ["competition_archive_id", "position"]},
        ),
        migrations.CreateModel(
            name="OfficialMatchArchive",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("competition_archive_type", models.CharField(choices=[("LEAGUE", "League competition"), ("FEDERATION", "Federation competition")], max_length=16)),
                ("competition_source_id", models.CharField(max_length=64)),
                ("round_number", models.PositiveSmallIntegerField()),
                ("leg", models.PositiveSmallIntegerField()),
                ("competition_type", models.CharField(max_length=16)),
                ("home_club_name_snapshot", models.CharField(max_length=160)),
                ("away_club_name_snapshot", models.CharField(max_length=160)),
                ("status", models.CharField(max_length=16)),
                ("result_type", models.CharField(max_length=20)),
                ("home_score", models.PositiveSmallIntegerField(blank=True, null=True)),
                ("away_score", models.PositiveSmallIntegerField(blank=True, null=True)),
                ("completed_at", models.DateTimeField(blank=True, null=True)),
                ("simulation_report_id", models.UUIDField(blank=True, null=True)),
                ("simulation_report_hash", models.CharField(blank=True, default="", max_length=64)),
                ("analytics_run_id", models.UUIDField(blank=True, null=True)),
                ("analytics_output_hash", models.CharField(blank=True, default="", max_length=64)),
                ("evidence_payload", models.JSONField(default=dict)),
                ("archive_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("away_club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="official_away_match_archives", to="clubs.club")),
                ("home_club", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="official_home_match_archives", to="clubs.club")),
                ("match", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="official_history_entries", to="matches.match")),
                ("projection", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="matches", to="official_history.officialhistoryprojection")),
            ],
            options={"ordering": ["projection_id", "competition_archive_type", "competition_source_id", "round_number", "match_id"]},
        ),
        migrations.CreateModel(
            name="OfficialClubSeasonStatistic",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("club_name_snapshot", models.CharField(max_length=160)),
                ("matches", models.PositiveIntegerField(default=0)),
                ("wins", models.PositiveIntegerField(default=0)),
                ("draws", models.PositiveIntegerField(default=0)),
                ("losses", models.PositiveIntegerField(default=0)),
                ("goals_for", models.PositiveIntegerField(default=0)),
                ("goals_against", models.PositiveIntegerField(default=0)),
                ("clean_sheets", models.PositiveIntegerField(default=0)),
                ("yellow_cards", models.PositiveIntegerField(default=0)),
                ("red_cards", models.PositiveIntegerField(default=0)),
                ("competition_breakdown", models.JSONField(default=dict)),
                ("statistic_payload", models.JSONField(default=dict)),
                ("statistic_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="official_season_statistics", to="clubs.club")),
                ("projection", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="club_statistics", to="official_history.officialhistoryprojection")),
            ],
            options={"ordering": ["projection_id", "club_id"]},
        ),
        migrations.CreateModel(
            name="OfficialPlayerSeasonStatistic",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("player_name_snapshot", models.CharField(max_length=220)),
                ("nationality_snapshot", models.CharField(blank=True, default="", max_length=100)),
                ("position_snapshot", models.CharField(blank=True, default="", max_length=8)),
                ("appearances", models.PositiveIntegerField(default=0)),
                ("starts", models.PositiveIntegerField(default=0)),
                ("minutes", models.PositiveIntegerField(default=0)),
                ("goals", models.PositiveIntegerField(default=0)),
                ("assists", models.PositiveIntegerField(default=0)),
                ("clean_sheets", models.PositiveIntegerField(default=0)),
                ("yellow_cards", models.PositiveIntegerField(default=0)),
                ("red_cards", models.PositiveIntegerField(default=0)),
                ("motm_awards", models.PositiveIntegerField(default=0)),
                ("xg", models.DecimalField(decimal_places=3, default=0, max_digits=14)),
                ("xa", models.DecimalField(decimal_places=3, default=0, max_digits=14)),
                ("average_rating", models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)),
                ("club_breakdown", models.JSONField(default=dict)),
                ("competition_breakdown", models.JSONField(default=dict)),
                ("statistic_payload", models.JSONField(default=dict)),
                ("statistic_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("player", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="official_season_statistics", to="players.player")),
                ("projection", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="player_statistics", to="official_history.officialhistoryprojection")),
            ],
            options={"ordering": ["projection_id", "player_id"]},
        ),
        migrations.CreateModel(
            name="OfficialHonor",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("honor_key", models.CharField(max_length=220)),
                ("honor_type", models.CharField(choices=[("LEAGUE_CHAMPION", "League champion"), ("LEAGUE_RUNNER_UP", "League runner-up"), ("CUP_CHAMPION", "Competition champion"), ("CUP_RUNNER_UP", "Competition runner-up"), ("TOP_SCORER", "Season top scorer"), ("TOP_ASSISTS", "Season top assists"), ("MOST_MOTM", "Most player-of-match awards"), ("HIGHEST_RATING", "Highest average rating")], max_length=32)),
                ("entity_type", models.CharField(choices=[("CLUB", "Club"), ("PLAYER", "Player"), ("YOUTH_TEAM", "Youth team")], max_length=16)),
                ("entity_reference", models.CharField(blank=True, default="", max_length=80)),
                ("entity_name_snapshot", models.CharField(max_length=220)),
                ("competition_archive_type", models.CharField(blank=True, choices=[("LEAGUE", "League competition"), ("FEDERATION", "Federation competition")], default="", max_length=16)),
                ("competition_source_id", models.CharField(blank=True, default="", max_length=64)),
                ("rank", models.PositiveSmallIntegerField(default=1)),
                ("metric_key", models.CharField(blank=True, default="", max_length=80)),
                ("metric_value", models.DecimalField(blank=True, decimal_places=4, max_digits=20, null=True)),
                ("evidence_payload", models.JSONField(default=dict)),
                ("honor_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="official_honors", to="clubs.club")),
                ("player", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="official_honors", to="players.player")),
                ("projection", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="honors", to="official_history.officialhistoryprojection")),
            ],
            options={"ordering": ["projection_id", "honor_type", "honor_key"]},
        ),
        migrations.CreateModel(
            name="OfficialHistoricalRecord",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("metric_key", models.CharField(choices=[("PLAYER_GOALS_SEASON", "Player goals in one season"), ("PLAYER_ASSISTS_SEASON", "Player assists in one season"), ("PLAYER_MOTM_SEASON", "Player MOTM awards in one season"), ("PLAYER_MINUTES_SEASON", "Player minutes in one season"), ("CLUB_WINS_SEASON", "Club wins in one season"), ("CLUB_GOALS_SEASON", "Club goals in one season"), ("CLUB_GOAL_DIFFERENCE_SEASON", "Club goal difference in one season"), ("LEAGUE_POINTS", "League points in one season")], max_length=48)),
                ("scope_key", models.CharField(default="GLOBAL", max_length=120)),
                ("entity_type", models.CharField(choices=[("CLUB", "Club"), ("PLAYER", "Player"), ("YOUTH_TEAM", "Youth team")], max_length=16)),
                ("entity_reference", models.CharField(blank=True, default="", max_length=80)),
                ("entity_name_snapshot", models.CharField(max_length=220)),
                ("value", models.DecimalField(decimal_places=4, max_digits=20)),
                ("unit", models.CharField(blank=True, default="", max_length=32)),
                ("relation", models.CharField(choices=[("INITIAL", "Initial record"), ("BROKE", "Broke previous record"), ("TIED", "Tied existing record")], max_length=12)),
                ("evidence_payload", models.JSONField(default=dict)),
                ("record_hash", models.CharField(max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="official_records", to="clubs.club")),
                ("player", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="official_records", to="players.player")),
                ("previous_record", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="successor_records", to="official_history.officialhistoricalrecord")),
                ("projection", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="records", to="official_history.officialhistoryprojection")),
            ],
            options={"ordering": ["metric_key", "scope_key", "projection__season_id", "id"]},
        ),
        migrations.CreateModel(
            name="OfficialHallOfFameEntry",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("entity_type", models.CharField(choices=[("CLUB", "Club"), ("PLAYER", "Player")], max_length=16)),
                ("entity_name_snapshot", models.CharField(max_length=220)),
                ("induction_reason", models.TextField()),
                ("evidence_payload", models.JSONField(default=dict)),
                ("evidence_hash", models.CharField(max_length=64)),
                ("entry_hash", models.CharField(max_length=64)),
                ("inducted_at", models.DateTimeField(auto_now_add=True)),
                ("club", models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="hall_of_fame_entry", to="clubs.club")),
                ("inducted_by", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="official_hall_of_fame_inductions", to=settings.AUTH_USER_MODEL)),
                ("inducted_season", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="hall_of_fame_inductions", to="seasons.season")),
                ("player", models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name="hall_of_fame_entry", to="players.player")),
                ("source_projection", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="hall_of_fame_inductions", to="official_history.officialhistoryprojection")),
            ],
            options={"ordering": ["-inducted_at", "id"]},
        ),
        migrations.AddConstraint(model_name="officialhistoryprojection", constraint=models.UniqueConstraint(fields=("ratification", "sequence"), name="official_hist_projection_seq_uq")),
        migrations.AddConstraint(model_name="officialhistoryprojection", constraint=models.UniqueConstraint(fields=("ratification", "source_chain_head_hash", "history_context_hash"), name="official_hist_projection_source_uq")),
        migrations.AddConstraint(model_name="officialhistoryprojection", constraint=models.CheckConstraint(condition=models.Q(("sequence__gte", 1)), name="official_hist_projection_seq_pos")),
        migrations.AddConstraint(model_name="officialhistoryprojection", constraint=models.CheckConstraint(condition=models.Q(("schema_version__gte", 1)), name="official_hist_projection_schema_pos")),
        migrations.AddConstraint(model_name="officialhistoryprojection", constraint=models.CheckConstraint(condition=~models.Q(projection_hash=""), name="official_hist_projection_hash_nonempty")),
        migrations.AddIndex(model_name="officialhistoryprojection", index=models.Index(fields=["season", "sequence"], name="official_hist_season_seq_idx")),
        migrations.AddIndex(model_name="officialhistoryprojection", index=models.Index(fields=["source_chain_head_hash"], name="official_hist_chain_hash_idx")),
        migrations.AddConstraint(model_name="officialcompetitionarchive", constraint=models.UniqueConstraint(fields=("projection", "archive_type", "source_id"), name="official_comp_archive_uq")),
        migrations.AddConstraint(model_name="officialcompetitionarchive", constraint=models.CheckConstraint(condition=~models.Q(archive_hash=""), name="official_comp_archive_hash_nonempty")),
        migrations.AddIndex(model_name="officialcompetitionarchive", index=models.Index(fields=["archive_type", "source_id"], name="official_comp_source_idx")),
        migrations.AddConstraint(model_name="officialleaguestandingrow", constraint=models.UniqueConstraint(fields=("competition_archive", "position"), name="official_standing_position_uq")),
        migrations.AddConstraint(model_name="officialleaguestandingrow", constraint=models.UniqueConstraint(fields=("competition_archive", "club"), name="official_standing_club_uq")),
        migrations.AddConstraint(model_name="officialleaguestandingrow", constraint=models.CheckConstraint(condition=models.Q(("position__gte", 1)), name="official_standing_position_pos")),
        migrations.AddConstraint(model_name="officialleaguestandingrow", constraint=models.CheckConstraint(condition=~models.Q(row_hash=""), name="official_standing_hash_nonempty")),
        migrations.AddConstraint(model_name="officialmatcharchive", constraint=models.UniqueConstraint(fields=("projection", "match"), name="official_match_archive_uq")),
        migrations.AddConstraint(model_name="officialmatcharchive", constraint=models.CheckConstraint(condition=~models.Q(archive_hash=""), name="official_match_archive_hash_nonempty")),
        migrations.AddIndex(model_name="officialmatcharchive", index=models.Index(fields=["match"], name="official_match_id_idx")),
        migrations.AddIndex(model_name="officialmatcharchive", index=models.Index(fields=["competition_archive_type", "competition_source_id"], name="official_match_comp_idx")),
        migrations.AddConstraint(model_name="officialclubseasonstatistic", constraint=models.UniqueConstraint(fields=("projection", "club"), name="official_club_season_stat_uq")),
        migrations.AddConstraint(model_name="officialclubseasonstatistic", constraint=models.CheckConstraint(condition=~models.Q(statistic_hash=""), name="official_club_stat_hash_nonempty")),
        migrations.AddConstraint(model_name="officialplayerseasonstatistic", constraint=models.UniqueConstraint(fields=("projection", "player"), name="official_player_season_stat_uq")),
        migrations.AddConstraint(model_name="officialplayerseasonstatistic", constraint=models.CheckConstraint(condition=~models.Q(statistic_hash=""), name="official_player_stat_hash_nonempty")),
        migrations.AddIndex(model_name="officialplayerseasonstatistic", index=models.Index(fields=["player", "projection"], name="official_player_projection_idx")),
        migrations.AddConstraint(model_name="officialhonor", constraint=models.UniqueConstraint(fields=("projection", "honor_key"), name="official_honor_key_uq")),
        migrations.AddConstraint(model_name="officialhonor", constraint=models.CheckConstraint(condition=models.Q(("rank__gte", 1)), name="official_honor_rank_pos")),
        migrations.AddConstraint(model_name="officialhonor", constraint=models.CheckConstraint(condition=~models.Q(honor_hash=""), name="official_honor_hash_nonempty")),
        migrations.AddIndex(model_name="officialhonor", index=models.Index(fields=["honor_type", "club"], name="official_honor_club_idx")),
        migrations.AddIndex(model_name="officialhonor", index=models.Index(fields=["honor_type", "player"], name="official_honor_player_idx")),
        migrations.AddConstraint(model_name="officialhistoricalrecord", constraint=models.UniqueConstraint(fields=("projection", "metric_key", "scope_key", "entity_type", "entity_reference"), name="official_record_projection_entity_uq")),
        migrations.AddConstraint(model_name="officialhistoricalrecord", constraint=models.CheckConstraint(condition=~models.Q(record_hash=""), name="official_record_hash_nonempty")),
        migrations.AddIndex(model_name="officialhistoricalrecord", index=models.Index(fields=["metric_key", "scope_key", "value"], name="official_record_metric_value_idx")),
        migrations.AddConstraint(model_name="officialhalloffameentry", constraint=models.CheckConstraint(condition=(models.Q(entity_type="CLUB", club__isnull=False, player__isnull=True) | models.Q(entity_type="PLAYER", player__isnull=False, club__isnull=True)), name="official_hof_entity_consistent")),
        migrations.AddConstraint(model_name="officialhalloffameentry", constraint=models.CheckConstraint(condition=~models.Q(entry_hash=""), name="official_hof_entry_hash_nonempty")),
    ]
