# Generated by Django 5.0.8 on 2024-08-31 08:09

from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps


def backfill_creator_id_and_date_created_from_realm_audit_log(
    apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
    RealmAuditLog = apps.get_model("zerver", "RealmAuditLog")
    RealmAuditLog.USER_GROUP_CREATED = 701
    NamedUserGroup = apps.get_model("zerver", "NamedUserGroup")

    user_group_creator_updates = []
    for audit_log_entry in RealmAuditLog.objects.select_related("modified_user_group").filter(
        event_type=RealmAuditLog.USER_GROUP_CREATED,
        acting_user_id__isnull=False,
    ):
        assert audit_log_entry.modified_user_group is not None
        user_group = audit_log_entry.modified_user_group
        user_group.creator_id = audit_log_entry.acting_user_id
        user_group_creator_updates.append(user_group)

    NamedUserGroup.objects.bulk_update(user_group_creator_updates, ["creator_id"], batch_size=1000)

    user_group_date_created_updates = []
    for audit_log_entry in RealmAuditLog.objects.select_related("modified_user_group").filter(
        event_type=RealmAuditLog.USER_GROUP_CREATED,
        event_time__isnull=False,
    ):
        assert audit_log_entry.modified_user_group is not None
        user_group = audit_log_entry.modified_user_group
        user_group.date_created = audit_log_entry.event_time
        user_group_date_created_updates.append(user_group)

    NamedUserGroup.objects.bulk_update(
        user_group_date_created_updates, ["date_created"], batch_size=1000
    )


class Migration(migrations.Migration):
    dependencies = [
        ("zerver", "0583_namedusergroup_creator_namedusergroup_date_created"),
    ]

    operations = [
        migrations.RunPython(
            backfill_creator_id_and_date_created_from_realm_audit_log,
            reverse_code=migrations.RunPython.noop,
            elidable=True,
        ),
    ]
