# Generated by Django 5.2.1 on 2025-08-26 17:05

from django.db import migrations, models


def populate_url(apps, schema_editor):
    MODELS = [
        'User',
        'Group',
        'Package',
        'Compound',
        'CompoundStructure',
        'Pathway',
        'Edge',
        'Node',
        'Reaction',
        'SimpleAmbitRule',
        'SimpleRDKitRule',
        'ParallelRule',
        'SequentialRule',
        'Scenario',
        'Setting',
        'MLRelativeReasoning',
        'EnviFormer',
        'ApplicabilityDomain',
    ]
    for model in MODELS:
        obj_cls = apps.get_model("epdb", model)
        for obj in obj_cls.objects.all():
            obj.url = assemble_url(obj)
            if obj.url is None:
                raise ValueError(f"Could not assemble url for {obj}")
            obj.save()


def assemble_url(obj):
    from django.conf import settings as s
    match obj.__class__.__name__:
        case 'User':
            return '{}/user/{}'.format(s.SERVER_URL, obj.uuid)
        case 'Group':
            return '{}/group/{}'.format(s.SERVER_URL, obj.uuid)
        case 'Package':
            return '{}/package/{}'.format(s.SERVER_URL, obj.uuid)
        case 'Compound':
            return '{}/compound/{}'.format(obj.package.url, obj.uuid)
        case 'CompoundStructure':
            return '{}/structure/{}'.format(obj.compound.url, obj.uuid)
        case 'SimpleAmbitRule':
            return '{}/simple-ambit-rule/{}'.format(obj.package.url, obj.uuid)
        case 'SimpleRDKitRule':
            return '{}/simple-rdkit-rule/{}'.format(obj.package.url, obj.uuid)
        case 'ParallelRule':
            return '{}/parallel-rule/{}'.format(obj.package.url, obj.uuid)
        case 'SequentialRule':
            return '{}/sequential-rule/{}'.format(obj.compound.url, obj.uuid)
        case 'Reaction':
            return '{}/reaction/{}'.format(obj.package.url, obj.uuid)
        case 'Pathway':
            return '{}/pathway/{}'.format(obj.package.url, obj.uuid)
        case 'Node':
            return '{}/node/{}'.format(obj.pathway.url, obj.uuid)
        case 'Edge':
            return '{}/edge/{}'.format(obj.pathway.url, obj.uuid)
        case 'MLRelativeReasoning':
            return '{}/model/{}'.format(obj.package.url, obj.uuid)
        case 'EnviFormer':
            return '{}/model/{}'.format(obj.package.url, obj.uuid)
        case 'ApplicabilityDomain':
            return '{}/model/{}/applicability-domain/{}'.format(obj.model.package.url, obj.model.uuid, obj.uuid)
        case 'Scenario':
            return '{}/scenario/{}'.format(obj.package.url, obj.uuid)
        case 'Setting':
            return '{}/setting/{}'.format(s.SERVER_URL, obj.uuid)
        case _:
            raise ValueError(f"Unknown model {obj.__class__.__name__}")


class Migration(migrations.Migration):
    dependencies = [
        ('epdb', '0002_externaldatabase_alter_apitoken_options_and_more'),
    ]

    operations = [
        migrations.AddField(
            model_name='applicabilitydomain',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='compound',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='compoundstructure',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='edge',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='epmodel',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='group',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='node',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='package',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='pathway',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='reaction',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='rule',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='scenario',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='setting',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),
        migrations.AddField(
            model_name='user',
            name='url',
            field=models.TextField(null=True, unique=False, verbose_name='URL'),
        ),

        migrations.RunPython(populate_url, reverse_code=migrations.RunPython.noop),

        migrations.AlterField(
            model_name='applicabilitydomain',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='compound',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='compoundstructure',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='edge',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='epmodel',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='group',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='node',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='package',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='pathway',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='reaction',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='rule',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='scenario',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='setting',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
        migrations.AlterField(
            model_name='user',
            name='url',
            field=models.TextField(null=True, unique=True, verbose_name='URL'),
        ),
    ]
