# Generated by Django 5.1.12 on 2025-10-02 05:45

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


def migrate_backchannel_logout_uri(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    OAuth2Provider = apps.get_model("authentik_providers_oauth2", "OAuth2Provider")

    db_alias = schema_editor.connection.alias
    for provider in OAuth2Provider.objects.using(db_alias).all():
        provider.logout_uri = provider.backchannel_logout_uri
        provider.logout_method = "backchannel"
        provider.save(update_fields=["logout_uri", "logout_method"])


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_providers_oauth2", "0030_oauth2provider_refresh_token_threshold"),
    ]

    operations = [
        migrations.AddField(
            model_name="oauth2provider",
            name="logout_method",
            field=models.TextField(
                choices=[("backchannel", "Back-channel"), ("frontchannel", "Front-channel")],
                default="backchannel",
                help_text="Backchannel logs out with server to server calls. Frontchannel uses iframes in your browser",
                verbose_name="Logout Method",
            ),
        ),
        migrations.AddField(
            model_name="oauth2provider",
            name="logout_uri",
            field=models.TextField(
                blank=True,
                validators=[authentik.lib.models.DomainlessURLValidator(schemes=("http", "https"))],
                verbose_name="Logout URI",
            ),
        ),
        migrations.RunPython(migrate_backchannel_logout_uri, lambda *args: ...),
        migrations.RemoveField(
            model_name="oauth2provider",
            name="backchannel_logout_uri",
        ),
    ]
