# Generated by Django 5.2.5 on 2025-08-17 06:25

from django.db import migrations

# The new value we are migrating to
NEW_SERVICE_TYPE = "frankfurter"
# The old values we are deprecating
OLD_SERVICE_TYPE_TO_UPDATE = "synth_finance"
OLD_SERVICE_TYPE_TO_DELETE = "synth_finance_stock"


def forwards_func(apps, schema_editor):
    """
    Forward migration:
    - Deletes all ExchangeRateService instances with service_type 'synth_finance_stock'.
    - Updates all ExchangeRateService instances with service_type 'synth_finance' to 'frankfurter'.
    """
    ExchangeRateService = apps.get_model("currencies", "ExchangeRateService")
    db_alias = schema_editor.connection.alias

    # 1. Delete the SYNTH_FINANCE_STOCK entries
    ExchangeRateService.objects.using(db_alias).filter(
        service_type=OLD_SERVICE_TYPE_TO_DELETE
    ).delete()

    # 2. Update the SYNTH_FINANCE entries to FRANKFURTER
    ExchangeRateService.objects.using(db_alias).filter(
        service_type=OLD_SERVICE_TYPE_TO_UPDATE
    ).update(service_type=NEW_SERVICE_TYPE, api_key=None)


def backwards_func(apps, schema_editor):
    """
    Backward migration: This operation is not safely reversible.
    - We cannot know which 'frankfurter' services were originally 'synth_finance'.
    - The deleted 'synth_finance_stock' services cannot be recovered.
    We will leave this function empty to allow migrating backwards without doing anything.
    """
    pass


class Migration(migrations.Migration):

    dependencies = [
        # Add the previous migration file here
        ("currencies", "0019_alter_exchangerateservice_service_type"),
    ]

    operations = [
        migrations.RunPython(forwards_func, reverse_code=backwards_func),
    ]
