# Generated by Django 5.0.6 on 2024-07-25 11:10

from django.db import migrations


def copy_supplier_to_provider(apps, schema_editor):
    providers = apps.get_model("circuits", "Provider")
    segments = apps.get_model("cesnet_service_path_plugin", "Segment")

    for segment in segments.objects.all():
        tenant = segment.supplier
        try:
            provider = providers.objects.get(custom_field_data__tenant=tenant.id)
        except providers.DoesNotExist:
            raise Exception(
                f"Provider not found. Please ensure the tenant {tenant.id} - {tenant.name} has an associated provider."
            )
        segment.provider_id = provider.id
        segment.save()


class Migration(migrations.Migration):
    dependencies = [
        (
            "cesnet_service_path_plugin",
            "0009_rename_supplier_segment_contract_segment_provider_segment_contract_and_more",
        ),
    ]

    operations = [
        migrations.RunPython(copy_supplier_to_provider),
    ]
