"""
Data migration: seed DeliveryArea with all Kenyan delivery zones.

Zones mirror the frontend DELIVERY_ZONES constant so slug/name/region/price
are in sync without any hardcoding on the client side.

estimated_days:
  Nairobi same-day/next-day areas   → 1
  Nairobi suburbs / satellite towns → 2
  Central region                    → 2–3
  Other regions                     → 3–5
  "Other" / unspecified             → 0 (confirm with team)
"""

from django.db import migrations, models


ZONES = [
    # slug                  name                              region          price  days
    ("nairobi-cbd",        "Nairobi CBD",                    "Nairobi",       200,   1),
    ("nairobi-westlands",  "Westlands & Parklands",          "Nairobi",       250,   1),
    ("nairobi-karen",      "Karen & Langata",                "Nairobi",       300,   1),
    ("nairobi-eastlands",  "Eastlands (Umoja / Kayole)",     "Nairobi",       250,   1),
    ("nairobi-south",      "South B & South C",              "Nairobi",       250,   1),
    ("nairobi-north",      "Kasarani & Roysambu",            "Nairobi",       300,   2),
    ("nairobi-ruiru",      "Ruiru & Juja",                   "Nairobi",       350,   2),
    ("kiambu",             "Kiambu Town",                    "Central",       350,   2),
    ("thika",              "Thika",                          "Central",       400,   2),
    ("nyeri",              "Nyeri",                          "Central",       500,   3),
    ("muranga",            "Murang'a",                       "Central",       500,   3),
    ("machakos",           "Machakos",                       "Eastern",       400,   2),
    ("embu",               "Embu",                           "Eastern",       550,   3),
    ("meru",               "Meru",                           "Eastern",       600,   4),
    ("nakuru",             "Nakuru",                         "Rift Valley",   500,   3),
    ("eldoret",            "Eldoret",                        "Rift Valley",   650,   4),
    ("kericho",            "Kericho",                        "Rift Valley",   600,   3),
    ("kisumu",             "Kisumu",                         "Nyanza",        700,   4),
    ("kakamega",           "Kakamega",                       "Western",       700,   4),
    ("bungoma",            "Bungoma",                        "Western",       750,   4),
    ("mombasa",            "Mombasa",                        "Coast",         700,   4),
    ("malindi",            "Malindi",                        "Coast",         800,   5),
    ("other",              "Other (confirm with us)",        "Other",           0,   0),
]


def seed_delivery_areas(apps, schema_editor):
    DeliveryArea = apps.get_model("store", "DeliveryArea")
    db = schema_editor.connection.alias

    # Remove any rows left over from earlier partial runs (slug=NULL or slug='')
    # so the unique constraint we add below can be applied cleanly.
    DeliveryArea.objects.using(db).filter(slug__isnull=True).delete()
    DeliveryArea.objects.using(db).filter(slug="").delete()

    for slug, name, region, price, days in ZONES:
        DeliveryArea.objects.using(db).update_or_create(
            slug=slug,
            defaults={
                "name": name,
                "county": region,   # model field is county; serializer exposes it as region
                "delivery_fee": price,
                "estimated_days": days,
                "is_active": True,
            },
        )


def reverse_seed(apps, schema_editor):
    DeliveryArea = apps.get_model("store", "DeliveryArea")
    db = schema_editor.connection.alias
    slugs = [row[0] for row in ZONES]
    DeliveryArea.objects.using(db).filter(slug__in=slugs).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("store", "0002_deliveryarea_slug"),
    ]

    operations = [
        # 1. Seed (and clean up stale rows)
        migrations.RunPython(seed_delivery_areas, reverse_code=reverse_seed),
        # 2. Now that every row has a unique slug, tighten the constraint
        migrations.AlterField(
            model_name="deliveryarea",
            name="slug",
            field=models.SlugField(max_length=100, unique=True),
        ),
    ]
