# Generated by Django 5.2 on 2025-05-25 22:01
import django.utils.timezone
from django.db import migrations, models

def convert_repeats_to_instances(apps, schema_editor):
    Episode = apps.get_model('app', 'Episode')
    HistoricalEpisode = apps.get_model('app', 'HistoricalEpisode')

    episodes_with_repeats = Episode.objects.filter(repeats__gt=0)

    for episode in episodes_with_repeats:
        history = HistoricalEpisode.objects.filter(
            id=episode.id
        ).order_by('history_date')

        # Create new episodes for each historical record including the current one
        for historical_record in history:
            new_episode = Episode(
            item=episode.item,
            related_season=episode.related_season,
            end_date=historical_record.end_date,
            repeats=0
            )
            new_episode.save()

            HistoricalEpisode.objects.create(
                id=new_episode.id,
                history_date=historical_record.history_date,
                history_type='+',
                history_user=historical_record.history_user,
                end_date=historical_record.end_date,
                repeats=0,
            )

        # Delete the original episode
        episode.delete()

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0044_fix_episode_images'),
    ]

    operations = [
        migrations.AddField(
            model_name='anime',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='basicmedia',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='book',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='comic',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='episode',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='game',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='manga',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='movie',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='season',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='tv',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AlterModelOptions(
            name='episode',
            options={},
        ),
        migrations.RemoveConstraint(
            model_name='episode',
            name='app_episode_unique_season_item',
        ),
        migrations.RunPython(
            convert_repeats_to_instances,
            reverse_code=migrations.RunPython.noop
        ),
    ]
