# Generated by Django 5.1.6 on 2025-03-08 12:53

from django.db import migrations, models

# Add functions from migration 0015
def convert_table_to_list(apps, schema_editor):
    User = apps.get_model('users', 'User')
    layout_fields = [
        'anime_layout',
        'book_layout',
        'game_layout',
        'manga_layout',
        'movie_layout',
        'season_layout',
        'tv_layout'
    ]

    # Update all users that have 'table' in any layout field
    for field in layout_fields:
        User.objects.filter(**{field: 'table'}).update(**{field: 'list'})


def reverse_convert_table_to_list(apps, schema_editor):
    User = apps.get_model('users', 'User')
    layout_fields = [
        'anime_layout',
        'book_layout',
        'game_layout',
        'manga_layout',
        'movie_layout',
        'season_layout',
        'tv_layout'
    ]

    # Reverse the operation by converting 'list' back to 'table'
    for field in layout_fields:
        User.objects.filter(**{field: 'list'}).update(**{field: 'table'})


# Add functions from migration 0018
def update_list_to_table_layouts(apps, schema_editor):
    """
    Update all user records that have 'list' layout to 'table' for all media types
    """
    User = apps.get_model('users', 'User')

    # Update all media type layouts from 'list' to 'table'
    layout_fields = [
        'tv_layout',
        'season_layout',
        'movie_layout',
        'anime_layout',
        'manga_layout',
        'game_layout',
        'book_layout'
    ]

    for field in layout_fields:
        User.objects.filter(**{field: 'list'}).update(**{field: 'table'})


def reverse_table_to_list_layouts(apps, schema_editor):
    """
    Reverse migration: change 'table' back to 'list' for all media types
    """
    User = apps.get_model('users', 'User')

    layout_fields = [
        'tv_layout',
        'season_layout',
        'movie_layout',
        'anime_layout',
        'manga_layout',
        'game_layout',
        'book_layout'
    ]

    for field in layout_fields:
        User.objects.filter(**{field: 'table'}).update(**{field: 'list'})


class Migration(migrations.Migration):

    replaces = [('users', '0009_alter_user_options'), ('users', '0010_alter_user_last_search_type'), ('users', '0011_user_token'), ('users', '0012_alter_user_last_search_type'), ('users', '0013_user_book_layout_alter_user_last_search_type'), ('users', '0014_user_anime_enabled_user_book_enabled_and_more'), ('users', '0015_alter_user_anime_layout_alter_user_book_layout_and_more'), ('users', '0016_user_hide_from_search'), ('users', '0017_alter_user_last_search_type'), ('users', '0018_alter_user_anime_layout_alter_user_book_layout_and_more'), ('users', '0019_user_calendar_layout'), ('users', '0020_user_last_search_type_valid_user_tv_layout_valid_and_more'), ('users', '0021_user_lists_sort_user_lists_sort_valid'), ('users', '0022_alter_user_last_search_type')]

    dependencies = [
        ('auth', '0012_alter_user_first_name_max_length'),
        ('users', '0001_squashed_0008_user_game_layout_alter_user_last_search_type'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='user',
            options={'ordering': ['username']},
        ),
        migrations.AddField(
            model_name='user',
            name='token',
            field=models.CharField(blank=True, help_text='Token for external webhooks', max_length=32, null=True, unique=True),
        ),
        migrations.AlterField(
            model_name='user',
            name='last_search_type',
            field=models.CharField(choices=[('movie', 'Movie'), ('tv', 'TV Show'), ('season', 'Season'), ('episode', 'Episode'), ('anime', 'Anime'), ('manga', 'Manga'), ('game', 'Game'), ('book', 'Book')], default='tv', max_length=10),
        ),
        migrations.AddField(
            model_name='user',
            name='anime_enabled',
            field=models.BooleanField(default=True),
        ),
        migrations.AddField(
            model_name='user',
            name='book_enabled',
            field=models.BooleanField(default=True),
        ),
        migrations.AddField(
            model_name='user',
            name='game_enabled',
            field=models.BooleanField(default=True),
        ),
        migrations.AddField(
            model_name='user',
            name='manga_enabled',
            field=models.BooleanField(default=True),
        ),
        migrations.AddField(
            model_name='user',
            name='movie_enabled',
            field=models.BooleanField(default=True),
        ),
        migrations.AddField(
            model_name='user',
            name='season_enabled',
            field=models.BooleanField(default=True),
        ),
        migrations.AddField(
            model_name='user',
            name='tv_enabled',
            field=models.BooleanField(default=True),
        ),
        migrations.AlterField(
            model_name='user',
            name='anime_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('list', 'List')], default='list', max_length=20),
        ),
        migrations.AddField(
            model_name='user',
            name='book_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('list', 'List')], default='grid', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='game_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('list', 'List')], default='grid', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='manga_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('list', 'List')], default='list', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='movie_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('list', 'List')], default='grid', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='season_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('list', 'List')], default='grid', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='tv_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('list', 'List')], default='grid', max_length=20),
        ),
        migrations.RunPython(
            code=convert_table_to_list,
            reverse_code=reverse_convert_table_to_list,
        ),
        migrations.AddField(
            model_name='user',
            name='hide_from_search',
            field=models.BooleanField(default=True),
        ),
        migrations.AlterField(
            model_name='user',
            name='last_search_type',
            field=models.CharField(choices=[('tv', 'TV Show'), ('season', 'Season'), ('episode', 'Episode'), ('movie', 'Movie'), ('anime', 'Anime'), ('manga', 'Manga'), ('game', 'Game'), ('book', 'Book')], default='tv', max_length=10),
        ),
        migrations.AlterField(
            model_name='user',
            name='anime_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('table', 'Table')], default='table', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='book_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('table', 'Table')], default='grid', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='game_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('table', 'Table')], default='grid', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='manga_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('table', 'Table')], default='table', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='movie_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('table', 'Table')], default='grid', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='season_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('table', 'Table')], default='grid', max_length=20),
        ),
        migrations.AlterField(
            model_name='user',
            name='tv_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('table', 'Table')], default='grid', max_length=20),
        ),
        migrations.RunPython(
            code=update_list_to_table_layouts,
            reverse_code=reverse_table_to_list_layouts,
        ),
        migrations.AddField(
            model_name='user',
            name='calendar_layout',
            field=models.CharField(choices=[('grid', 'Grid'), ('list', 'List')], default='grid', max_length=20),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('last_search_type__in', ['tv', 'season', 'episode', 'movie', 'anime', 'manga', 'game', 'book'])), name='last_search_type_valid'),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('tv_layout__in', ['grid', 'table'])), name='tv_layout_valid'),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('season_layout__in', ['grid', 'table'])), name='season_layout_valid'),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('movie_layout__in', ['grid', 'table'])), name='movie_layout_valid'),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('anime_layout__in', ['grid', 'table'])), name='anime_layout_valid'),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('manga_layout__in', ['grid', 'table'])), name='manga_layout_valid'),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('game_layout__in', ['grid', 'table'])), name='game_layout_valid'),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('book_layout__in', ['grid', 'table'])), name='book_layout_valid'),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('calendar_layout__in', ['grid', 'list'])), name='calendar_layout_valid'),
        ),
        migrations.AddField(
            model_name='user',
            name='lists_sort',
            field=models.CharField(choices=[('last_item_added', 'Last Item Added'), ('name', 'Name'), ('items_count', 'Items Count'), ('creation_order', 'Newest First')], default='last_item_added', max_length=20),
        ),
        migrations.AddConstraint(
            model_name='user',
            constraint=models.CheckConstraint(condition=models.Q(('lists_sort__in', ['last_item_added', 'name', 'items_count', 'creation_order'])), name='lists_sort_valid'),
        ),
        migrations.AlterField(
            model_name='user',
            name='last_search_type',
            field=models.CharField(choices=[('tv', 'TV Show'), ('season', 'TV Season'), ('episode', 'Episode'), ('movie', 'Movie'), ('anime', 'Anime'), ('manga', 'Manga'), ('game', 'Game'), ('book', 'Book')], default='tv', max_length=10),
        ),
    ]
