# Generated by Django 4.2.4 on 2023-09-26 14:58
from django.db import migrations


def migrate_data(apps, schema_editor):
    Event = apps.get_model("event", "Event")
    EventSettings = apps.get_model("event", "Event_SettingsStore")
    Submission = apps.get_model("submission", "Submission")
    MediaCccDeLink = apps.get_model("pretalx_media_ccc_de", "MediaCccDeLink")
    for event in Event.objects.all().filter(plugins__contains="pretalx_media_ccc_de"):
        settings = {
            s.key: s.value
            for s in EventSettings.objects.filter(
                object=event, key__startswith="media_ccc_de_url_"
            )
        }
        for key, value in settings.items():
            submission = Submission.objects.filter(
                event=event, code=key.split("_")[-1]
            ).first()
            if submission:
                MediaCccDeLink.objects.create(submission=submission, url=value)


def delete_all_links(apps, schema_editor):
    MediaCccDeLink = apps.get_model("pretalx_media_ccc_de", "MediaCccDeLink")
    MediaCccDeLink.objects.all().delete()


class Migration(migrations.Migration):
    dependencies = [
        ("pretalx_media_ccc_de", "0001_initial"),
    ]

    operations = [migrations.RunPython(migrate_data, migrations.RunPython.noop)]
