# Generated by Django 5.1 on 2024-12-13 13:04

from django.db import migrations

def init_store_id(apps, schema_editor):
    GroupStage = apps.get_model("tournaments", "GroupStage")
    for instance in GroupStage.objects.all():
        instance.store_id = str(instance.tournament.id)
        instance.save()

    Round = apps.get_model("tournaments", "Round")
    for instance in Round.objects.all():
        instance.store_id = str(instance.tournament.id)
        instance.save()

    TeamRegistration = apps.get_model("tournaments", "TeamRegistration")
    for instance in TeamRegistration.objects.all():
        instance.store_id = str(instance.tournament.id)
        instance.save()

    PlayerRegistration = apps.get_model("tournaments", "PlayerRegistration")
    for instance in PlayerRegistration.objects.all():
        instance.store_id = str(instance.team_registration.tournament.id)
        instance.save()

    Match = apps.get_model("tournaments", "Match")
    for instance in Match.objects.all():
        if instance.round:
            id = instance.round.tournament.id
        else:
            id = instance.group_stage.tournament.id
        instance.store_id = str(id)
        instance.save()

    TeamScore = apps.get_model("tournaments", "TeamScore")
    for instance in TeamScore.objects.all():
        if instance.team_registration:
            id = instance.team_registration.tournament.id
        else:
            match = instance.match
            if match.round:
                id = match.round.tournament.id
            else:
                id = match.group_stage.tournament.id
        instance.store_id = str(id)
        instance.save()

    DrawLog = apps.get_model("tournaments", "DrawLog")
    for instance in DrawLog.objects.all():
        instance.store_id = str(instance.tournament.id)
        instance.save()

def init_related_names(apps, schema_editor):
    models = ["Club", "Event"]
    for model in models:
        Model = apps.get_model("tournaments", model)
        for instance in Model.objects.all():
            instance.related_user = instance.creator
            instance.save()

class Migration(migrations.Migration):

    dependencies = [
        ('tournaments', '0108_club_creation_date_club_last_update_and_more'),
    ]

    operations = [
        migrations.RunPython(init_store_id),
        migrations.RunPython(init_related_names),
    ]
