# Generated by Django 3.2.19 on 2023-05-17 08:44

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
from django.core.exceptions import ObjectDoesNotExist


def migrate_old_data(apps, schema_editor):
    EventChecklist = apps.get_model('RIGS', 'EventChecklist')
    EventCheckIn = apps.get_model('RIGS', 'EventCheckIn')
    for ec in EventChecklist.objects.all():
        for crew in ec.crew.all():
            try:
                EventCheckIn.objects.create(event=ec.event, person=crew.crewmember, role=crew.role, time=crew.start, end_time=crew.end, vehicle=ec.vehicles.get(driver=crew.crewmember).vehicle)
            except ObjectDoesNotExist:
                EventCheckIn.objects.create(event=ec.event, person=crew.crewmember, role=crew.role, time=crew.start, end_time=crew.end)


def revert(apps, schema_editor):
    apps.get_model('RIGS', 'EventCheckIn').objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('RIGS', '0046_create_powertests'),
    ]

    operations = [
        migrations.CreateModel(
            name='EventCheckIn',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('time', models.DateTimeField()),
                ('role', models.CharField(blank=True, max_length=50)),
                ('vehicle', models.CharField(blank=True, max_length=100)),
                ('end_time', models.DateTimeField(blank=True, null=True)),
                ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='crew', to='RIGS.event')),
                ('person', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='checkins', to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.RunPython(migrate_old_data, reverse_code=revert),
    ]
