# Generated by Django 4.2.9 on 2024-11-25 12:03

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import uuid

from funkwhale_api.federation import utils
from django.urls import reverse


def get_user_actor(apps, schema_editor):
    MyModel = apps.get_model("playlists", "Playlist")
    for row in MyModel.objects.all():
        actor = row.user.actor
        row.actor = actor
        row.save(update_fields=["actor"])


def gen_uuid(apps, schema_editor):
    MyModel = apps.get_model("playlists", "Playlist")
    for row in MyModel.objects.all():
        unique_uuid = uuid.uuid4()
        while MyModel.objects.filter(uuid=unique_uuid).exists():
            unique_uuid = uuid.uuid4()

        fid = utils.full_url(
            reverse("federation:music:playlist-detail", kwargs={"uuid": unique_uuid})
        )
        row.uuid = unique_uuid
        row.fid = fid
        row.save(update_fields=["uuid", "fid"])


class Migration(migrations.Migration):
    dependencies = [
        ("federation", "0028_auto_20221027_1141"),
        ("playlists", "0004_auto_20180320_1713"),
    ]

    operations = [
        migrations.AddField(
            model_name="playlist",
            name="fid",
            field=models.URLField(max_length=500            ),
        ),
        migrations.AddField(
            model_name="playlist",
            name="url",
            field=models.URLField(blank=True, max_length=500, null=True),
        ),
        migrations.AddField(
            model_name="playlist",
            name="uuid",
            field=models.UUIDField(default=uuid.uuid4, null=True),
        ),
        migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop),
        migrations.AlterField(
            model_name="playlist",
            name="uuid",
            field=models.UUIDField(default=uuid.uuid4, null=False, unique=True),
        ),
        migrations.AlterField(
            model_name="playlist",
            name="fid",
            field=models.URLField(max_length=500, unique=True, db_index=True,
            ),),
        migrations.AddField(
            model_name="playlist",
            name="actor",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="playlists",
                to="federation.actor",
                null=True,
                blank=True,
            ),
        ),
        migrations.RunPython(get_user_actor, reverse_code=migrations.RunPython.noop),
        migrations.AlterField(
            model_name="playlist",
            name="actor",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="track_favorites",
                to="federation.actor",
            ),
        ),
        migrations.RemoveField(
            model_name="playlist",
            name="user",
        ),
    ]
