# Generated by Django 5.0.13 on 2025-03-17 08:49

import django.db.models.deletion
from django.db import migrations, models


from django.apps.registry import Apps

from django.db.backends.base.schema import BaseDatabaseSchemaEditor

from authentik.events.models import EventAction


def make_many_groups(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    """Adds the Group object in SCIMProvider.filter_group to the
    many-to-many relationship in SCIMProvider.group_filters"""
    db_alias = schema_editor.connection.alias

    from authentik.lib.migrations import migration_event

    SCIMProvider = apps.get_model("authentik_providers_scim", "scimprovider")

    for provider in SCIMProvider.objects.using(db_alias).all():
        if not provider.filter_group:
            continue
        provider.group_filters.add(provider.filter_group)
        provider.dry_run = True
        provider.save(update_fields=["dry_run"])
        migration_event(
            apps,
            schema_editor,
            EventAction.CONFIGURATION_WARNING,
            message=(
                "SCIM Providers' `filter_group` has been removed in favor of `group_filters`. Your configuration has been migrated."
                "To prevent users/groups from being removed, the provider's dry-run mode has been enabled. Please review "
                " its configuration and disable the dry-run mode."
            ),
            provider=provider,
        )


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_core", "0043_alter_group_options"),
        ("authentik_providers_scim", "0018_scimprovider_service_provider_config_cache_timeout"),
    ]

    operations = [
        migrations.AddField(
            model_name="scimprovider",
            name="group_filters",
            field=models.ManyToManyField(
                blank=True,
                default=None,
                help_text="Group filters used to define sync-scope for groups.",
                related_name="groups",
                to="authentik_core.group",
            ),
        ),
        migrations.AlterField(
            model_name="scimprovider",
            name="filter_group",
            field=models.ForeignKey(
                default=None,
                null=True,
                on_delete=django.db.models.deletion.SET_DEFAULT,
                related_name="group",
                to="authentik_core.group",
            ),
        ),
        migrations.RunPython(make_many_groups),
        migrations.RemoveField(
            model_name="scimprovider",
            name="filter_group",
        ),
        migrations.AlterField(
            model_name="scimprovider",
            name="group_filters",
            field=models.ManyToManyField(
                blank=True,
                default=None,
                help_text="Group filters used to define sync-scope for groups.",
                to="authentik_core.group",
            ),
        ),
        migrations.AlterField(
            model_name="scimprovider",
            name="compatibility_mode",
            field=models.CharField(
                choices=[
                    ("default", "Default"),
                    ("aws", "AWS"),
                    ("slack", "Slack"),
                    ("sfdc", "Salesforce"),
                    ("webex", "Webex"),
                ],
                default="default",
                help_text="Alter authentik behavior for vendor-specific SCIM implementations.",
                max_length=30,
                verbose_name="SCIM Compatibility Mode",
            ),
        ),
    ]
