# Generated by Django 4.2.8 on 2023-12-19 11:28

from django.db import migrations, models, transaction
from django.utils import dateparse
import csv


def add_to_ORM(HeightPercentile: models.Model, alias, filepath: str, sex: str):
    with open(filepath) as csvfile:
        height_reader = csv.DictReader(csvfile)
        data_list = []
        for row in height_reader:
            data_list.append(
                HeightPercentile(
                    age_in_days=dateparse.parse_duration(f'P{row["Day"]}D'),
                    sex=sex,
                    p3_height=row["P3"],
                    p15_height=row["P15"],
                    p50_height=row["P50"],
                    p85_height=row["P85"],
                    p97_height=row["P97"],
                )
            )
        HeightPercentile.objects.using(alias).bulk_create(data_list, batch_size=50)


def insert_height_percentiles(apps, schema_editor):
    HeightPercentile: models.Model = apps.get_model("core", "HeightPercentile")
    db_alias = schema_editor.connection.alias
    with transaction.atomic():
        add_to_ORM(
            HeightPercentile,
            db_alias,
            "core/migrations/height_percentile_boys.csv",
            "boy",
        )
    with transaction.atomic():
        add_to_ORM(
            HeightPercentile,
            db_alias,
            "core/migrations/height_percentile_girls.csv",
            "girl",
        )


def remove_height_percentiles(apps, schema_editor):
    HeightPercentile: models.Model = apps.get_model("core", "HeightPercentile")
    db_alias = schema_editor.connection.alias
    with transaction.atomic():
        HeightPercentile.objects.using(db_alias).all().delete()


class Migration(migrations.Migration):
    dependencies = [
        ("core", "0032_child_birth_time"),
    ]

    operations = [
        migrations.CreateModel(
            name="HeightPercentile",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("age_in_days", models.DurationField()),
                ("p3_height", models.FloatField()),
                ("p15_height", models.FloatField()),
                ("p50_height", models.FloatField()),
                ("p85_height", models.FloatField()),
                ("p97_height", models.FloatField()),
                (
                    "sex",
                    models.CharField(
                        choices=[("girl", "Girl"), ("boy", "Boy")], max_length=255
                    ),
                ),
            ],
        ),
        migrations.AddConstraint(
            model_name="heightpercentile",
            constraint=models.UniqueConstraint(
                fields=("age_in_days", "sex"), name="unique_age_sex_height"
            ),
        ),
        migrations.RunPython(insert_height_percentiles, remove_height_percentiles),
    ]
