# Generated by Django 3.2.3 on 2021-06-06 16:29

from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def migrate_mode(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    from authentik.providers.proxy.models import ProxyMode

    db_alias = schema_editor.connection.alias
    ProxyProvider = apps.get_model("authentik_providers_proxy", "proxyprovider")
    for provider in ProxyProvider.objects.using(db_alias).all():
        if provider.forward_auth_mode:
            provider.mode = ProxyMode.FORWARD_SINGLE
            provider.save()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_providers_proxy", "0012_proxyprovider_cookie_domain"),
    ]

    operations = [
        migrations.AddField(
            model_name="proxyprovider",
            name="mode",
            field=models.TextField(
                choices=[
                    ("proxy", "Proxy"),
                    ("forward_single", "Forward Single"),
                    ("forward_domain", "Forward Domain"),
                ],
                default="proxy",
                help_text="Enable support for forwardAuth in traefik and nginx auth_request. Exclusive with internal_host.",
            ),
        ),
        migrations.RunPython(migrate_mode),
        migrations.RemoveField(
            model_name="proxyprovider",
            name="forward_auth_mode",
        ),
    ]
