# Generated by Django 4.2.5 on 2023-09-20 10:56

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


def add_to_ORM(WeightPercentile: models.Model, alias, filepath: str, sex: str):
    with open(filepath) as csvfile:
        weight_reader = csv.DictReader(csvfile)
        data_list = []
        for row in weight_reader:
            data_list.append(
                WeightPercentile(
                    age_in_days=dateparse.parse_duration(f'P{row["Age"]}D'),
                    sex=sex,
                    p3_weight=row["P3"],
                    p15_weight=row["P15"],
                    p50_weight=row["P50"],
                    p85_weight=row["P85"],
                    p97_weight=row["P97"],
                )
            )
        WeightPercentile.objects.using(alias).bulk_create(data_list, batch_size=50)


def insert_weight_percentiles(apps, schema_editor):
    WeightPercentile: models.Model = apps.get_model("core", "WeightPercentile")
    db_alias = schema_editor.connection.alias
    with transaction.atomic():
        add_to_ORM(
            WeightPercentile,
            db_alias,
            "core/migrations/weight_percentile_boys.csv",
            "boy",
        )
    with transaction.atomic():
        add_to_ORM(
            WeightPercentile,
            db_alias,
            "core/migrations/weight_percentile_girls.csv",
            "girl",
        )


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


class Migration(migrations.Migration):
    dependencies = [
        ("core", "0029_alter_pumping_options_remove_pumping_time_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="WeightPercentile",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("age_in_days", models.DurationField()),
                ("p3_weight", models.FloatField()),
                ("p15_weight", models.FloatField()),
                ("p50_weight", models.FloatField()),
                ("p85_weight", models.FloatField()),
                ("p97_weight", models.FloatField()),
                (
                    "sex",
                    models.CharField(
                        choices=[("girl", "Girl"), ("boy", "Boy")], max_length=255
                    ),
                ),
            ],
        ),
        migrations.AddConstraint(
            model_name="weightpercentile",
            constraint=models.UniqueConstraint(
                fields=("age_in_days", "sex"), name="unique_age_sex"
            ),
        ),
        migrations.RunPython(insert_weight_percentiles, remove_weight_percentiles),
    ]
