# Generated by Django 5.1.5 on 2025-05-09 17:29

import aleksis.core.mixins
import ckeditor.fields
import colorfield.fields
import django.contrib.postgres.fields
import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models

def migrate_availability_events(apps, schema_editor):
    AvailabilityEvent = apps.get_model("core", "AvailabilityEvent")
    AvailabilityType = apps.get_model("core", "AvailabilityType")

    free_availability_type = AvailabilityType.objects.create(name="Free", short_name="f", free=True, public=True, managed_by_app_label="core")
    busy_availability_type = AvailabilityType.objects.create(name="Busy", short_name="b", free=False, public=True, managed_by_app_label="core")

    for a in AvailabilityEvent.objects.all():
        if a.free:
            a.availability_type = free_availability_type
        else:
            a.availability_type = busy_availability_type
        a.save()


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0087_add_uuid'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='AvailabilityType',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('managed_by_app_label', models.CharField(blank=True, editable=False, max_length=255, verbose_name='App label of app responsible for managing this instance')),
                ('uuid', models.UUIDField(db_default=models.Func(function='gen_random_uuid'), editable=False)),
                ('extended_data', models.JSONField(default=dict, editable=False)),
                ('name', models.CharField(max_length=100, verbose_name='Name')),
                ('short_name', models.CharField(max_length=100, verbose_name='Short name')),
                ('description', models.TextField(blank=True, verbose_name='Description')),
                ('color', colorfield.fields.ColorField(blank=True, null=True, default='', image_field=None, max_length=25, samples=None, verbose_name='Color')),
                ('free', models.BooleanField(help_text='This indicates whether availability events with this type should count as free.', verbose_name='Free')),
                ('public', models.BooleanField(help_text='This indicates whether this type should be publicly available.', verbose_name='Public')),
            ],
            options={
                'verbose_name': 'Availability type',
                'verbose_name_plural': 'Availability types',
            },
        ),
        migrations.AddField(
            model_name='availabilityevent',
            name='availability_type',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='availability_events', to='core.availabilitytype', verbose_name='Availability type'),
        ),
        migrations.RunPython(migrate_availability_events),
        migrations.RemoveField(
            model_name='availabilityevent',
            name='free',
        ),
        migrations.AlterField(
            model_name='availabilityevent',
            name='availability_type',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='availability_events', to='core.availabilitytype', verbose_name='Availability type'),
        ),
    ]
