# Generated by Django 4.0.8 on 2025-07-14 13:40

from django.db import migrations
from django.utils.timezone import now


def move_sets_to_userlists(apps, schema_editor):
    UserList = apps.get_model('social', 'UserList')
    UserListItem = apps.get_model('social', 'UserListItem')
    Tag = apps.get_model('catalogue', 'Tag')

    for tag in Tag.objects.filter(category='set'):
        print()
        print(tag)
        ul = UserList.objects.create(
            slug=tag.slug,
            user=tag.user,
            name=tag.name,
            favorites=not tag.name,
            public=not tag.name,
            created_at=tag.created_at,
            updated_at=tag.changed_at,
        )

        for i, item in enumerate(tag.items.all()):
            #assert item.content_type_id == 12, item.content_type_id
            print(item)
            ul.userlistitem_set.create(
                order=i + 1,
                created_at=ul.updated_at,
                updated_at=ul.updated_at,
                book_id=item.object_id
            )

        tag.delete()


def rollback_userlists_to_sets(apps, schema_editor):
    UserList = apps.get_model('social', 'UserList')
    UserListItem = apps.get_model('social', 'UserListItem')
    Tag = apps.get_model('catalogue', 'Tag')


class Migration(migrations.Migration):

    dependencies = [
        ('social', '0020_userlist_userlistitem'),
        ('catalogue', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(
            move_sets_to_userlists,
            rollback_userlists_to_sets
        )
    ]
