from django.db import migrations


def regenerate_callback_urls(apps, schema_editor):
    Organization = apps.get_model('accounts', 'Organization')
    PlatformSettings = apps.get_model('accounts', 'PlatformSettings')

    settings_obj, _ = PlatformSettings.objects.get_or_create(pk=1)
    base = settings_obj.ovse_base_url.rstrip('/') if settings_obj.ovse_base_url else 'https://www.as4.in'
    fixed_url = f"{base}/verify-response/"

    Organization.objects.update(callback_url=fixed_url)


def noop_reverse(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0007_platformsettings_uidai_credentials'),
    ]

    operations = [
        migrations.RunPython(regenerate_callback_urls, noop_reverse),
    ]
