# Generated by Django 4.2.9 on 2024-03-28 23:32

import uuid

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

from django.conf import settings

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


def gen_uuid(apps, schema_editor):
    MyModel = apps.get_model("favorites", "TrackFavorite")
    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:likes-detail", kwargs={"uuid": unique_uuid})
        )
        row.uuid = unique_uuid
        row.fid = fid
        row.save(update_fields=["uuid", "fid"])


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



class Migration(migrations.Migration):
    dependencies = [
        ("favorites", "0002_trackfavorite_source"),
    ]

    operations = [
        migrations.AddField(
            model_name="trackfavorite",
            name="actor",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="track_favorites",
                to="federation.actor",
            ),
        ),
        migrations.AddField(
            model_name="trackfavorite",
            name="fid",
            field=models.URLField(
                db_index=True,
                default="https://default.fid",
                max_length=500,
                unique=True,
            ),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="trackfavorite",
            name="url",
            field=models.URLField(blank=True, max_length=500, null=True),
        ),
        migrations.AddField(
            model_name="trackfavorite",
            name="uuid",
            field=models.UUIDField(null=True),
        ),
        migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop),
        migrations.AlterField(
            model_name="trackfavorite",
            name="uuid",
            field=models.UUIDField(default=uuid.uuid4, unique=True, null=False),
        ),
        migrations.RunPython(get_user_actor, reverse_code=migrations.RunPython.noop),
        migrations.AlterField(
            model_name="trackfavorite",
            name="actor",
            field=models.ForeignKey(
                blank=False,
                null=False,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="track_favorites",
                to="federation.actor",
            ),      ),
        migrations.AlterUniqueTogether(
            name="trackfavorite",
            unique_together={("track", "actor")},
        ),
        migrations.RemoveField(
            model_name="trackfavorite",
            name="user",
        ),

    ]
