from django.db import migrations


def seed_reference_data(apps, schema_editor):
    IstatGuestType = apps.get_model("istat", "IstatGuestType")
    IstatGender = apps.get_model("istat", "IstatGender")
    IstatTourismType = apps.get_model("istat", "IstatTourismType")
    IstatTransportType = apps.get_model("istat", "IstatTransportType")

    guest_types = [
        ("16", "Single Guest"),
        ("17", "Head of Family"),
        ("18", "Group Leader"),
        ("19", "Family Member"),
        ("20", "Group Member"),
    ]
    genders = [
        ("1", "Male"),
        ("2", "Female"),
    ]
    tourism_types = [
        "Cultural",
        "Beach",
        "Conference/Business",
        "Trade Fair",
        "Sport/Fitness",
        "School",
        "Religious",
        "Social",
        "Theme Parks",
        "Spa/Health Treatments",
        "Food and Wine",
        "Cycle Tourism",
        "Excursion/Nature",
        "Other reason",
        "Not Specified",
    ]
    transport_types = [
        "Car",
        "Plane",
        "Plane + Bus",
        "Plane + Shuttle/Taxi/Car",
        "Plane + Train",
        "Train",
        "Bus",
        "Caravan",
        "Boat",
        "Motorcycle",
        "Bicycle",
        "Walking",
        "Other",
        "Not Specified",
    ]

    IstatGuestType.objects.bulk_create(
        [IstatGuestType(code=code, description=desc) for code, desc in guest_types],
        ignore_conflicts=True,
    )
    IstatGender.objects.bulk_create(
        [IstatGender(code=code, meaning=meaning) for code, meaning in genders],
        ignore_conflicts=True,
    )
    IstatTourismType.objects.bulk_create(
        [IstatTourismType(name=name) for name in tourism_types],
        ignore_conflicts=True,
    )
    IstatTransportType.objects.bulk_create(
        [IstatTransportType(name=name) for name in transport_types],
        ignore_conflicts=True,
    )


def unseed_reference_data(apps, schema_editor):
    IstatGuestType = apps.get_model("istat", "IstatGuestType")
    IstatGender = apps.get_model("istat", "IstatGender")
    IstatTourismType = apps.get_model("istat", "IstatTourismType")
    IstatTransportType = apps.get_model("istat", "IstatTransportType")

    IstatGuestType.objects.filter(code__in=["16", "17", "18", "19", "20"]).delete()
    IstatGender.objects.filter(code__in=["1", "2"]).delete()
    IstatTourismType.objects.filter(
        name__in=[
            "Cultural",
            "Beach",
            "Conference/Business",
            "Trade Fair",
            "Sport/Fitness",
            "School",
            "Religious",
            "Social",
            "Theme Parks",
            "Spa/Health Treatments",
            "Food and Wine",
            "Cycle Tourism",
            "Excursion/Nature",
            "Other reason",
            "Not Specified",
        ]
    ).delete()
    IstatTransportType.objects.filter(
        name__in=[
            "Car",
            "Plane",
            "Plane + Bus",
            "Plane + Shuttle/Taxi/Car",
            "Plane + Train",
            "Train",
            "Bus",
            "Caravan",
            "Boat",
            "Motorcycle",
            "Bicycle",
            "Walking",
            "Other",
            "Not Specified",
        ]
    ).delete()


class Migration(migrations.Migration):
    dependencies = [
        ("istat", "0001_initial"),
    ]

    operations = [
        migrations.RunPython(seed_reference_data, unseed_reference_data),
    ]
