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

import django.core.validators
import django.db.models.deletion
import simple_history.models
from django.conf import settings
from django.db import migrations, models
from django.db.models import Case, When, Value, CharField 


def forward_func(apps, _):
    Item = apps.get_model('app', 'Item')

    # Define the cases for updating the 'source' field
    source_cases = [
        When(media_type__in=["tv", "season", "episode", "movie"], then=Value("tmdb")),
        When(media_type__in=["manga", "anime"], then=Value("mal")),
        When(media_type="game", then=Value("igdb")),
    ]

    # Perform the bulk update
    Item.objects.update(
        source=Case(
            *source_cases,
            default=Value(""),
            output_field=CharField()
        )
    )

def reverse_func(apps, _):
    Item = apps.get_model('app', 'Item')

    # Perform the bulk update
    Item.objects.update(source="")

class Migration(migrations.Migration):

    replaces = [('app', '0022_item_source'), ('app', '0023_alter_item_source'), ('app', '0024_alter_item_unique_together'), ('app', '0025_alter_item_episode_number_alter_item_media_type_and_more'), ('app', '0026_alter_anime_item_alter_game_item_alter_manga_item_and_more'), ('app', '0027_alter_item_source'), ('app', '0028_alter_item_unique_together_and_more'), ('app', '0029_alter_anime_status_alter_game_status_and_more'), ('app', '0030_alter_item_image'), ('app', '0031_alter_item_image'), ('app', '0032_alter_item_media_id_alter_item_media_type_and_more'), ('app', '0033_rename_watch_date_episode_end_date_and_more'), ('app', '0034_alter_anime_status_alter_book_status_and_more'), ('app', '0035_alter_anime_unique_together_and_more'), ('app', '0036_item_app_item_source_valid_and_more'), ('app', '0037_alter_item_media_type')]

    dependencies = [
        ('app', '0015_customlist_listitem_squashed_0021_alter_item_media_type'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name='item',
            name='source',
            field=models.CharField(default='', max_length=255),
        ),
        migrations.RunPython(
            code=forward_func,
            reverse_code=reverse_func,
        ),
        migrations.AlterField(
            model_name='item',
            name='source',
            field=models.CharField(max_length=255),
        ),
        migrations.AlterUniqueTogether(
            name='item',
            unique_together={('media_id', 'source', 'media_type', 'season_number', 'episode_number')},
        ),
        migrations.AlterField(
            model_name='item',
            name='episode_number',
            field=models.PositiveIntegerField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='item',
            name='media_type',
            field=models.CharField(choices=[('movie', 'Movie'), ('tv', 'TV Show'), ('season', 'Season'), ('episode', 'Episode'), ('anime', 'Anime'), ('manga', 'Manga'), ('game', 'Game')], default='movie', max_length=10),
        ),
        migrations.AlterField(
            model_name='item',
            name='season_number',
            field=models.PositiveIntegerField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='anime',
            name='item',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.item'),
        ),
        migrations.AlterField(
            model_name='game',
            name='item',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.item'),
        ),
        migrations.AlterField(
            model_name='manga',
            name='item',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.item'),
        ),
        migrations.AlterField(
            model_name='movie',
            name='item',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.item'),
        ),
        migrations.AlterField(
            model_name='season',
            name='item',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.item'),
        ),
        migrations.AlterField(
            model_name='tv',
            name='item',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.item'),
        ),
        migrations.AlterField(
            model_name='item',
            name='source',
            field=models.CharField(choices=[('tmdb', 'tmdb'), ('mal', 'mal'), ('mangaupdates', 'mangaupdates'), ('igdb', 'igdb'), ('manual', 'manual')], max_length=20),
        ),
        migrations.AlterUniqueTogether(
            name='item',
            unique_together=set(),
        ),
        migrations.AddConstraint(
            model_name='item',
            constraint=models.UniqueConstraint(condition=models.Q(('episode_number__isnull', True), ('season_number__isnull', True)), fields=('media_id', 'source', 'media_type'), name='unique_item_without_season_episode'),
        ),
        migrations.AddConstraint(
            model_name='item',
            constraint=models.UniqueConstraint(condition=models.Q(('episode_number__isnull', True), ('season_number__isnull', False)), fields=('media_id', 'source', 'media_type', 'season_number'), name='unique_item_with_season'),
        ),
        migrations.AddConstraint(
            model_name='item',
            constraint=models.UniqueConstraint(condition=models.Q(('episode_number__isnull', False), ('season_number__isnull', False)), fields=('media_id', 'source', 'media_type', 'season_number', 'episode_number'), name='unique_item_with_season_episode'),
        ),
        migrations.AddConstraint(
            model_name='item',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('episode_number__isnull', True), ('media_type', 'season'), ('season_number__isnull', False)), models.Q(('media_type', 'season'), _negated=True), _connector='OR'), name='season_number_required_for_season'),
        ),
        migrations.AddConstraint(
            model_name='item',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('episode_number__isnull', False), ('media_type', 'episode'), ('season_number__isnull', False)), models.Q(('media_type', 'episode'), _negated=True), _connector='OR'), name='season_and_episode_required_for_episode'),
        ),
        migrations.AddConstraint(
            model_name='item',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(models.Q(('media_type__in', ['season', 'episode']), _negated=True), ('episode_number__isnull', True), ('season_number__isnull', True)), ('media_type__in', ['season', 'episode']), _connector='OR'), name='no_season_episode_for_other_types'),
        ),
        migrations.AlterField(
            model_name='item',
            name='image',
            field=models.URLField(),
        ),
        migrations.AlterField(
            model_name='item',
            name='media_id',
            field=models.CharField(max_length=20),
        ),
        migrations.AlterField(
            model_name='item',
            name='media_type',
            field=models.CharField(choices=[('tv', 'TV Show'), ('season', 'Season'), ('episode', 'Episode'), ('movie', 'Movie'), ('anime', 'Anime'), ('manga', 'Manga'), ('game', 'Game'), ('book', 'Book')], default='movie', max_length=10),
        ),
        migrations.AlterField(
            model_name='item',
            name='source',
            field=models.CharField(choices=[('tmdb', 'The Movie Database'), ('mal', 'MyAnimeList'), ('mangaupdates', 'MangaUpdates'), ('igdb', 'Internet Game Database'), ('openlibrary', 'Open Library'), ('manual', 'Manual')], max_length=20),
        ),
        migrations.RenameField(
            model_name='episode',
            old_name='watch_date',
            new_name='end_date',
        ),
        migrations.RenameField(
            model_name='historicalepisode',
            old_name='watch_date',
            new_name='end_date',
        ),
        migrations.AlterField(
            model_name='anime',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='game',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='historicalanime',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.CreateModel(
            name='HistoricalBook',
            fields=[
                ('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
                ('score', models.DecimalField(blank=True, decimal_places=1, max_digits=3, null=True, validators=[django.core.validators.DecimalValidator(3, 1), django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(10)])),
                ('progress', models.PositiveIntegerField(default=0)),
                ('status', models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20)),
                ('repeats', models.PositiveIntegerField(default=0)),
                ('start_date', models.DateField(blank=True, null=True)),
                ('end_date', models.DateField(blank=True, null=True)),
                ('notes', models.TextField(blank=True, default='')),
                ('history_id', models.AutoField(primary_key=True, serialize=False)),
                ('history_date', models.DateTimeField(db_index=True)),
                ('history_change_reason', models.CharField(max_length=100, null=True)),
                ('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
                ('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'historical book',
                'verbose_name_plural': 'historical books',
                'ordering': ('-history_date', '-history_id'),
                'get_latest_by': ('history_date', 'history_id'),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.AlterField(
            model_name='historicalgame',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='historicalmanga',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='historicalmovie',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='historicalseason',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='historicaltv',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='manga',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='movie',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='season',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.AlterField(
            model_name='tv',
            name='status',
            field=models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20),
        ),
        migrations.CreateModel(
            name='HistoricalBasicMedia',
            fields=[
                ('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
                ('score', models.DecimalField(blank=True, decimal_places=1, max_digits=3, null=True, validators=[django.core.validators.DecimalValidator(3, 1), django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(10)])),
                ('progress', models.PositiveIntegerField(default=0)),
                ('status', models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20)),
                ('repeats', models.PositiveIntegerField(default=0)),
                ('start_date', models.DateField(blank=True, null=True)),
                ('end_date', models.DateField(blank=True, null=True)),
                ('notes', models.TextField(blank=True, default='')),
                ('history_id', models.AutoField(primary_key=True, serialize=False)),
                ('history_date', models.DateTimeField(db_index=True)),
                ('history_change_reason', models.CharField(max_length=100, null=True)),
                ('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
                ('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'historical basic media',
                'verbose_name_plural': 'historical basic medias',
                'ordering': ('-history_date', '-history_id'),
                'get_latest_by': ('history_date', 'history_id'),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.AlterUniqueTogether(
            name='anime',
            unique_together=set(),
        ),
        migrations.CreateModel(
            name='BasicMedia',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('score', models.DecimalField(blank=True, decimal_places=1, max_digits=3, null=True, validators=[django.core.validators.DecimalValidator(3, 1), django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(10)])),
                ('progress', models.PositiveIntegerField(default=0)),
                ('status', models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20)),
                ('repeats', models.PositiveIntegerField(default=0)),
                ('start_date', models.DateField(blank=True, null=True)),
                ('end_date', models.DateField(blank=True, null=True)),
                ('notes', models.TextField(blank=True, default='')),
                ('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.item')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['-score'],
                'abstract': False,
                'unique_together': set(),
            },
        ),
        migrations.CreateModel(
            name='Book',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('score', models.DecimalField(blank=True, decimal_places=1, max_digits=3, null=True, validators=[django.core.validators.DecimalValidator(3, 1), django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(10)])),
                ('progress', models.PositiveIntegerField(default=0)),
                ('status', models.CharField(choices=[('Completed', 'Completed'), ('In progress', 'In Progress'), ('Repeating', 'Repeating'), ('Planning', 'Planning'), ('Paused', 'Paused'), ('Dropped', 'Dropped')], default='Completed', max_length=20)),
                ('repeats', models.PositiveIntegerField(default=0)),
                ('start_date', models.DateField(blank=True, null=True)),
                ('end_date', models.DateField(blank=True, null=True)),
                ('notes', models.TextField(blank=True, default='')),
                ('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.item')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['-score'],
                'abstract': False,
                'unique_together': set(),
            },
        ),
        migrations.AlterUniqueTogether(
            name='episode',
            unique_together=set(),
        ),
        migrations.AlterUniqueTogether(
            name='game',
            unique_together=set(),
        ),
        migrations.AlterUniqueTogether(
            name='manga',
            unique_together=set(),
        ),
        migrations.AlterUniqueTogether(
            name='movie',
            unique_together=set(),
        ),
        migrations.AlterUniqueTogether(
            name='season',
            unique_together=set(),
        ),
        migrations.AlterUniqueTogether(
            name='tv',
            unique_together=set(),
        ),
        migrations.AddConstraint(
            model_name='anime',
            constraint=models.UniqueConstraint(fields=('item', 'user'), name='app_anime_unique_item_user'),
        ),
        migrations.AddConstraint(
            model_name='basicmedia',
            constraint=models.UniqueConstraint(fields=('item', 'user'), name='app_basicmedia_unique_item_user'),
        ),
        migrations.AddConstraint(
            model_name='book',
            constraint=models.UniqueConstraint(fields=('item', 'user'), name='app_book_unique_item_user'),
        ),
        migrations.AddConstraint(
            model_name='episode',
            constraint=models.UniqueConstraint(fields=('related_season', 'item'), name='app_episode_unique_season_item'),
        ),
        migrations.AddConstraint(
            model_name='game',
            constraint=models.UniqueConstraint(fields=('item', 'user'), name='app_game_unique_item_user'),
        ),
        migrations.AddConstraint(
            model_name='manga',
            constraint=models.UniqueConstraint(fields=('item', 'user'), name='app_manga_unique_item_user'),
        ),
        migrations.AddConstraint(
            model_name='movie',
            constraint=models.UniqueConstraint(fields=('item', 'user'), name='app_movie_unique_item_user'),
        ),
        migrations.AddConstraint(
            model_name='season',
            constraint=models.UniqueConstraint(fields=('related_tv', 'item'), name='app_season_unique_tv_item'),
        ),
        migrations.AddConstraint(
            model_name='tv',
            constraint=models.UniqueConstraint(fields=('item', 'user'), name='app_tv_unique_item_user'),
        ),
        migrations.AddConstraint(
            model_name='item',
            constraint=models.CheckConstraint(condition=models.Q(('source__in', ['tmdb', 'mal', 'mangaupdates', 'igdb', 'openlibrary', 'manual'])), name='app_item_source_valid'),
        ),
        migrations.AddConstraint(
            model_name='item',
            constraint=models.CheckConstraint(condition=models.Q(('media_type__in', ['tv', 'season', 'episode', 'movie', 'anime', 'manga', 'game', 'book'])), name='app_item_media_type_valid'),
        ),
        migrations.AlterField(
            model_name='item',
            name='media_type',
            field=models.CharField(choices=[('tv', 'TV Show'), ('season', 'TV Season'), ('episode', 'Episode'), ('movie', 'Movie'), ('anime', 'Anime'), ('manga', 'Manga'), ('game', 'Game'), ('book', 'Book')], default='movie', max_length=10),
        ),
    ]
