# Generated by Django 3.1 on 2020-08-28 13:14
from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def add_title_for_defaults(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    slug_title_map = {
        "default-authentication-flow": "Welcome to authentik!",
        "default-invalidation-flow": "Default Invalidation Flow",
        "default-source-enrollment": "Welcome to authentik! Please select a username.",
        "default-source-authentication": "Welcome to authentik!",
        "default-provider-authorization-implicit-consent": "Default Provider Authorization Flow (implicit consent)",
        "default-provider-authorization-explicit-consent": "Default Provider Authorization Flow (explicit consent)",
        "default-password-change": "Change password",
    }
    db_alias = schema_editor.connection.alias
    Flow = apps.get_model("authentik_flows", "Flow")
    for flow in Flow.objects.using(db_alias).all():
        if flow.slug in slug_title_map:
            flow.title = slug_title_map[flow.slug]
        else:
            flow.title = flow.name
        flow.save()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_flows", "0010_provider_flows"),
    ]

    operations = [
        migrations.AlterModelOptions(
            name="flow",
            options={
                "permissions": [("export_flow", "Can export a Flow")],
                "verbose_name": "Flow",
                "verbose_name_plural": "Flows",
            },
        ),
        migrations.AddField(
            model_name="flow",
            name="title",
            field=models.TextField(default="", blank=True),
            preserve_default=False,
        ),
        migrations.RunPython(add_title_for_defaults),
        migrations.AlterField(
            model_name="flow",
            name="title",
            field=models.TextField(),
        ),
    ]
