# Generated by Django 3.2.15 on 2022-10-01 08:44

import datetime
from django.db import migrations, models
import django.db.models.deletion
from django.utils import timezone

import seminar.models as m


def vytvor_deadliny(apps, schema_editor):
    Cislo = apps.get_model('seminar', 'Cislo')
    Deadline = apps.get_model('seminar', 'Deadline')

    for cislo in Cislo.objects.all():
        if cislo.rocnik.rocnik < 26:
            Deadline.objects.create(
                cislo=cislo,
                typ=m.Deadline.TYP_CISLA,
                deadline=timezone.make_aware(datetime.datetime.combine(datetime.date(1994 + cislo.rocnik.rocnik, 6, int(cislo.poradi[0])), datetime.time.min)),
                verejna_vysledkovka=cislo.verejna_vysledkovka,
            )
            continue

        def vytvor_deadline(date: datetime.date, typ):
            Deadline.objects.create(
                cislo=cislo,
                typ=typ,
                deadline=timezone.make_aware(datetime.datetime.combine(date, datetime.time.min)) + datetime.timedelta(days=1),
                verejna_vysledkovka=True
            )

        if cislo.datum_deadline_soustredeni and cislo.datum_deadline_soustredeni == cislo.datum_preddeadline:
            vytvor_deadline(
                date=cislo.datum_deadline_soustredeni,
                typ=m.Deadline.TYP_PRVNI_A_SOUS
            )
        else:
            if cislo.datum_deadline_soustredeni:
                vytvor_deadline(
                    date=cislo.datum_deadline_soustredeni,
                    typ=m.Deadline.TYP_SOUS
                )
            if cislo.datum_preddeadline:
                vytvor_deadline(
                    date=cislo.datum_preddeadline,
                    typ=m.Deadline.TYP_PRVNI
                )

        if cislo.datum_deadline:
            vytvor_deadline(
                date=cislo.datum_deadline,
                typ=m.Deadline.TYP_CISLA
            )


class Migration(migrations.Migration):

    dependencies = [
        ('seminar', '0102_osoba_jak_se_dozvedeli'),
    ]

    operations = [
        migrations.CreateModel(
            name='Deadline',
            fields=[
                ('id', models.AutoField(primary_key=True, serialize=False)),
                ('deadline', models.DateTimeField(default=timezone.make_aware(datetime.datetime.combine(timezone.now(), datetime.time.max)))),
                ('typ', models.CharField(choices=[('cisla', 'Deadline celého čísla'), ('prvni', 'První deadline'), ('prvniasous', 'Sousový a první deadline'), ('sous', 'Sousový deadline')], max_length=32, verbose_name='typ deadlinu')),
                ('verejna_vysledkovka', models.BooleanField(db_column='verejna_vysledkovka', default=False, verbose_name='veřejná výsledkovka')),
                ('cislo', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='deadline_v_cisle', to='seminar.cislo', verbose_name='deadline v čísle')),
            ],
            options={
                'verbose_name': 'Deadline',
                'verbose_name_plural': 'Deadliny',
                'db_table': 'seminar_deadliny',
                'ordering': ['deadline'],
            },
        ),
        migrations.RunPython(vytvor_deadliny, migrations.RunPython.noop),
    ]