# Seed migration: populate initial weather parameters.

from django.db import migrations


INITIAL_PARAMETERS = [
    ("temperature_min", "حداقل دمای هوا", "°C"),
    ("temperature_max", "حداکثر دمای هوا", "°C"),
    ("temperature_mean", "میانگین دمای هوا", "°C"),
    ("precipitation", "مجموع بارش", "mm"),
    ("precipitation_probability", "احتمال بارش", "%"),
    ("humidity_mean", "میانگین رطوبت نسبی", "%"),
    ("wind_speed_max", "حداکثر سرعت باد", "km/h"),
    ("et0", "تبخیر-تعرق مرجع (ET₀)", "mm/day"),
    ("weather_code", "کد وضعیت آب‌وهوا (WMO)", ""),
]


def seed_parameters(apps, schema_editor):
    WeatherParameter = apps.get_model("weather", "WeatherParameter")
    for code, name_fa, unit in INITIAL_PARAMETERS:
        WeatherParameter.objects.get_or_create(
            code=code,
            defaults={"name_fa": name_fa, "unit": unit},
        )


def unseed_parameters(apps, schema_editor):
    WeatherParameter = apps.get_model("weather", "WeatherParameter")
    codes = [p[0] for p in INITIAL_PARAMETERS]
    WeatherParameter.objects.filter(code__in=codes).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("weather", "0001_initial"),
    ]

    operations = [
        migrations.RunPython(seed_parameters, unseed_parameters),
    ]
