# Generated by Django 4.2.7 on 2023-12-04 11:09


import logging

from django.db import migrations, models

logger = logging.getLogger(__name__)

# Mapping for updating ImpactLevel names based on value and impact_type
IMPACT_LEVEL_NAME_MAPPING = {
    # Format: (value, impact_type): (new_name, description)
    ("LT", "business_impact"): (
        "Business Lowest Not measurable",
        "Lowest business impact, not measurable."
    ),
    ("LO", "business_impact"): (
        "Business Lower Not measurable",
        "Lower business impact, not measurable."
    ),
    ("MD", "business_impact"): (
        "Uncertain Business Impact",
        "Uncertain business value daily loss."
    ),
    ("HI", "business_impact"): (
        "Major Business Impact (<5% BV daily loss)",
        "Major business impact, less than 5% business value daily loss."
    ),
    ("HT", "business_impact"): (
        "Critical Business Impact (>5% BV daily loss)",
        "Critical business impact, more than 5% business value daily loss."
    ),
    ("NO", "business_impact"): (
        "No impact on business.",
        ""
    ),
    ("LT", "customers_impact"): (
        "Cosmetic issues for few customers",
        "Minor functional affecting few users / cosmetic issues."
    ),
    ("LO", "customers_impact"): (
        "Minor functional issues for many customers",
        "Minor functional issues affecting many customers."
    ),
    ("MD", "customers_impact"): (
        "Major functional issues for many customers",
        "Many customer with major functional issues (no affecting BV)."
    ),
    ("HI", "customers_impact"): (
        "Some customers have major issues",
        "Purchase flow experience with major experience issues impacting <5% of customers.\nAccess to product not working affecting <5% of customers/errors\nMarketplace unstable <30% errors."
    ),
    ("HT", "customers_impact"): (
        "Critical issue for many customers",
        "Purchase flow experience with major experience issues impacting >5% of customers.\nAccess to product not working affecting >5% of customers/errors\nMarketplace unstable >30% errors."
    ),
    ("NO", "customers_impact"): (
        "No impact for customers.",
        ""
    ),
    ("LT", "sellers_impact"): (
        "Cosmetic issues for few sellers",
        "Minor cosmetic issues affecting few sellers."
    ),
    ("LO", "sellers_impact"): (
        "Minor functional issues for many sellers",
        "Minor functional issues affecting many sellers."
    ),
    ("MD", "sellers_impact"): (
        "Major functional issues for many sellers",
        "Many sellers with major functional issues (no affecting fulfillment / catalog availability / order).\nIssues affecting commercial operations (e.g. wrong pricing on inventory / offers / etc) of big part of our inventory (e.g. big / important sellers)."
    ),
    ("HI", "sellers_impact"): (
        "Some sellers have major issues",
        "Toolbox access hindered affecting <20% of sellers.\nImpossibility of publishing / modifying catalogue affecting <20% of sellers."
    ),
    ("HT", "sellers_impact"): (
        "Critical issue for many sellers",
        "Toolbox access hindered affecting >20% of sellers.\nImpossibility of publishing / modifying catalogue affecting >20% of sellers."
    ),
    ("NO", "sellers_impact"): (
        "No impact for sellers.",
        ""
    ),
    ("LT", "employees_impact"): (
        "Degraded service for MM workers or partners but not blocked",
        "Service is degraded for MM workers or partners, but they are not blocked."
    ),
    ("LO", "employees_impact"): (
        "Few MM workers or partners affected systematically or blocked",
        "A few MM workers or partners are systematically affected or blocked."
    ),
    ("MD", "employees_impact"): (
        "Full departments blocked on their work.",
        "Entire departments are blocked in their work."
    ),
    ("NO", "employees_impact"): (
        "No impact for employee.",
        ""
    ),
}


def update_impact_level_names(apps, _schema_editor):
    ImpactLevel = apps.get_model("incidents", "ImpactLevel")
    ImpactType = apps.get_model("incidents", "ImpactType")
    for (value, impact_type_value), (new_name, description) in IMPACT_LEVEL_NAME_MAPPING.items():
        try:
            impact_type = ImpactType.objects.filter(value=impact_type_value).first()
            if not impact_type:
                logger.warning(f"ImpactType '{impact_type}' does not exist. Skipping update for value '{value}' and impact_type '{impact_type_value}'.")
                continue

            impact_level = ImpactLevel.objects.get(value=value, impact_type=impact_type.id)
            impact_level.name = new_name
            impact_level.description = description
            impact_level.save()
            logger.info(f"Updated ImpactLevel ({value}, {impact_type}) to name '{new_name}'")
        except ImpactLevel.DoesNotExist:
            logger.warning(f"ImpactLevel with value '{value}' and impact_type '{impact_type}' does not exist.")


class Migration(migrations.Migration):

    dependencies = [
        ("incidents", "0014_update_components_slack_groups"),
    ]

    operations = [
        migrations.AddField(
            model_name="impactlevel",
            name="description",
            field=models.TextField(null=True, blank=True),
        ),
        migrations.RunPython(update_impact_level_names),
    ]
