# Generated by Django 3.2.7 on 2022-03-28 08:40

from django.db import migrations

# Migrate old impact values to new impact values
#
# Old values
# ECONOMIC_ASSESSMENT_IMPACT = (
#     (1, "1: £ less than a thousand"),
#     (2, "2: £ tens of thousands"),
#     (3, "3: £ hundreds of thousands"),
#     (4, "4: £ millions"),
#     (5, "5: £ tens of millions"),
#     (6, "6: £ hundreds of millions"),
#     (7, "7: £ billions"),
#     (8, "8: £ tens of billions"),
# )
# New values
# NEW_ECONOMIC_ASSESSMENT_IMPACT = (
#     (1, "1: £ thousands"),
#     (2, "2: £ tens of thousands"),
#     (3, "3: £ hundreds of thousands (£100k - £999k)"),
#     (4, "4: £ low hundreds of thousands (£100k-£400k)"),
#     (5, "5: £ mid hundreds of thousands (£400k-£700k)"),
#     (6, "6: £ high hundreds of thousands (£700k-£999k)"),
#     (7, "7: £ millions (£1m-£9.9m)"),
#     (8, "8: £ low millions (£1m-£4m)"),
#     (9, "9: £ mid millions (£4m-£7m)"),
#     (10, "10: £ high millions (£7m-£9.9m)"),
#     (11, "11: £ tens of millions (£10m-£99m)"),
#     (12, "12: £ low tens of millions (£10m-£40m)"),
#     (13, "13: £ mid tens of millions (£40m-£70m)"),
#     (14, "14: £ high tens of millions (£70m-£99m)"),
#     (15, "15: £ hundreds of millions (£100m-£999m)"),
#     (16, "16: £ low hundreds of millions (£100m-£400m)"),
#     (17, "17: £ mid hundreds of millions (£400m-£700m)"),
#     (18, "18: £ high hundreds of millions (£700m-£999m)"),
#     (19, "19: £ billions (£1bn-£9.9bn)"),
#     (20, "20: £ low billions (£1bn-£4bn)"),
#     (21, "21: £ mid billions (£4bn-£7bn)"),
#     (22, "22: £ high billions (£7bn-£9.9bn)"),
#     (23, "23: £ tens of billions (£10bn-£20bn)"),
#     (24, "24: £ low tens of billions (£10bn-£15bn)"),
#     (25, "25: £ high tens of billions (£15bn-£20bn)"),
# )

OLD_TO_NEW_ECONOMIC_ASSESSMENT_IMPACT_MAPPING = {
    1: 1,
    2: 2,
    3: 3,
    4: 7,
    5: 11,
    6: 15,
    7: 19,
    8: 23,
}


def migrate_old_to_new_impact_values(apps, schema_editor):
    """
    Migrate old impact values to new impact values.
    """
    EconomicImpactAssessment = apps.get_model("assessment", "EconomicImpactAssessment")
    HistoricalEconomicImpactAssessment = apps.get_model(
        "assessment", "HistoricalEconomicImpactAssessment"
    )
    # for each mapping, find the old value and update the new value
    for old_value, new_value in OLD_TO_NEW_ECONOMIC_ASSESSMENT_IMPACT_MAPPING.items():
        EconomicImpactAssessment.objects.filter(impact=old_value).update(
            impact=new_value,
        )
        HistoricalEconomicImpactAssessment.objects.filter(impact=old_value).update(
            impact=new_value
        )


def reverse_migrate_old_to_new_impact_values(apps, schema_editor):
    """
    Reverse migrate old impact values to new impact values.
    """
    EconomicImpactAssessment = apps.get_model("assessment", "EconomicImpactAssessment")
    HistoricalEconomicImpactAssessment = apps.get_model(
        "assessment", "HistoricalEconomicImpactAssessment"
    )
    # for each mapping, find the new value and update the old value
    for old_value, new_value in OLD_TO_NEW_ECONOMIC_ASSESSMENT_IMPACT_MAPPING.items():
        EconomicImpactAssessment.objects.filter(impact=new_value).update(
            impact=old_value,
        )
        HistoricalEconomicImpactAssessment.objects.filter(impact=new_value).update(
            impact=old_value
        )


class Migration(migrations.Migration):

    dependencies = [
        ("assessment", "0019_auto_20220325_1445"),
    ]

    operations = [
        migrations.RunPython(
            migrate_old_to_new_impact_values, reverse_migrate_old_to_new_impact_values
        ),
    ]
