# Generated by Django 4.2.11 on 2024-05-08 08:55

from django.conf import settings
import django.contrib.postgres.fields
from django.db import migrations, models
import django.db.models.deletion

from django_pg_rrule.operations import RruleExtension
from recurrence import serialize



def _migrate_recurrences(apps, schema_editor):
    CalendarEvent = apps.get_model('core', 'CalendarEvent')
    for event in CalendarEvent.objects.using(schema_editor.connection.alias).all():
        if not event.recurrences:
            continue
        event.exdatetimes = event.recurrences.exdates
        event.rdatetimes = event.recurrences.rdates
        event.rrule = serialize(event.recurrences.rrules[0])
        if event.rrule.startswith("RRULE:"):
            event.rrule = event.rrule[6:]
        event.save()

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('core', '0063_add_announcement_priority'),
    ]

    operations = [
        RruleExtension(),
        migrations.AddField(
            model_name='calendarevent',
            name='exdates',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.DateField(), blank=True, default=[], size=None),
        ),
        migrations.AddField(
            model_name='calendarevent',
            name='exdatetimes',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.DateTimeField(), blank=True, default=[], size=None),
        ),
        migrations.AddField(
            model_name='calendarevent',
            name='rdates',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.DateField(), blank=True, default=[], size=None),
        ),
        migrations.AddField(
            model_name='calendarevent',
            name='rdatetimes',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.DateTimeField(), blank=True, default=[], size=None),
        ),
        migrations.AddField(
            model_name='calendarevent',
            name='rrule',
            field=models.TextField(blank=True, null=True),
        ),
        migrations.RunPython(_migrate_recurrences)
    ]
