# Generated by Django 3.1.1 on 2020-09-15 19:53
from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor

import authentik.core.models


def create_default_admin_group(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    db_alias = schema_editor.connection.alias
    Group = apps.get_model("authentik_core", "Group")
    User = apps.get_model("authentik_core", "User")

    # Creates a default admin group
    group, _ = Group.objects.using(db_alias).get_or_create(
        is_superuser=True,
        defaults={
            "name": "authentik Admins",
        },
    )
    group.users.set(User.objects.filter(username="akadmin"))
    group.save()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_core", "0008_auto_20200824_1532"),
    ]

    operations = [
        migrations.RemoveField(
            model_name="user",
            name="is_superuser",
        ),
        migrations.RemoveField(
            model_name="user",
            name="is_staff",
        ),
        migrations.AlterField(
            model_name="user",
            name="pb_groups",
            field=models.ManyToManyField(
                related_name="users", to="authentik_core.Group"
            ),
        ),
        migrations.AddField(
            model_name="group",
            name="is_superuser",
            field=models.BooleanField(
                default=False, help_text="Users added to this group will be superusers."
            ),
        ),
        migrations.RunPython(create_default_admin_group),
        migrations.AlterModelManagers(
            name="user",
            managers=[
                ("objects", authentik.core.models.UserManager()),
            ],
        ),
    ]
