"""Seed the controlled Region vocabulary (legacy AGRIS "Region Focus", vid 22)."""

from django.db import migrations
from django.utils.text import slugify

# (display_order, name) — Africa-first ordering to match the institutional audience.
REGIONS = [
    "Africa Wide",
    "East Africa",
    "Central Africa",
    "Southern Africa",
    "West Africa",
    "North Africa",
    "Middle East",
    "Asia and Pacific",
    "Europe",
    "North America",
    "Central America",
    "South America",
]


def seed_regions(apps, schema_editor):
    Region = apps.get_model("repository_documents", "Region")
    for order, name in enumerate(REGIONS):
        Region.objects.update_or_create(
            slug=slugify(name),
            defaults={"name": name, "display_order": order},
        )


def unseed_regions(apps, schema_editor):
    Region = apps.get_model("repository_documents", "Region")
    Region.objects.filter(slug__in=[slugify(n) for n in REGIONS]).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("repository_documents", "0010_region_document_agris_identifier_and_more"),
    ]

    operations = [
        migrations.RunPython(seed_regions, unseed_regions),
    ]
