# Generated by Django 5.2 on 2025-05-15 18:55

from django.db import migrations, models
from django.db.models import Q
from django.utils.timezone import datetime, make_aware

def convert_date_to_datetime(apps, schema_editor):
    # List of all models with date fields being converted
    models_to_convert = [
        ('Anime', ['start_date', 'end_date']),
        ('HistoricalAnime', ['start_date', 'end_date']),
        ('Book', ['start_date', 'end_date']),
        ('HistoricalBook', ['start_date', 'end_date']),
        ('Comic', ['start_date', 'end_date']),
        ('HistoricalComic', ['start_date', 'end_date']),
        ('Episode', ['end_date']),  # Episode only has end_date
        ('HistoricalEpisode', ['end_date']),  # HistoricalEpisode only has end_date
        ('Game', ['start_date', 'end_date']),
        ('HistoricalGame', ['start_date', 'end_date']),
        ('Manga', ['start_date', 'end_date']),
        ('HistoricalManga', ['start_date', 'end_date']),
        ('Movie', ['start_date', 'end_date']),
        ('HistoricalMovie', ['start_date', 'end_date']),
    ]

    for model_name, date_fields in models_to_convert:
        Model = apps.get_model('app', model_name)

        # Create Q objects for each field to check if it's not null
        q_objects = Q()
        for field in date_fields:
            q_objects |= Q(**{f'{field}__isnull': False})

        # Get all instances that have at least one non-null date field
        instances = Model.objects.filter(q_objects)

        updates = []
        for instance in instances:
            updated = False
            for field in date_fields:
                date_value = getattr(instance, field)
                if date_value:
                    setattr(instance, field, make_aware(
                        datetime.combine(date_value, datetime.min.time())
                    ))
                    updated = True

            if updated:
                updates.append(instance)

        # Bulk update all changed instances
        if updates:
            Model.objects.bulk_update(updates, date_fields)



class Migration(migrations.Migration):

    dependencies = [
        ('app', '0040_remove_item_app_item_source_valid_alter_item_source_and_more'),
    ]

    operations = [
        migrations.AlterField(
            model_name='anime',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='anime',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='basicmedia',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='basicmedia',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='book',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='book',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='comic',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='comic',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='episode',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='game',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='game',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalanime',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalanime',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalbasicmedia',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalbasicmedia',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalbook',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalbook',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalcomic',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalcomic',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalepisode',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalgame',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalgame',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalmanga',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalmanga',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalmovie',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='historicalmovie',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='manga',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='manga',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='movie',
            name='end_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='movie',
            name='start_date',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.RunPython(
            convert_date_to_datetime,
            reverse_code=migrations.RunPython.noop,
        ),
    ]
