# Generated by Django 5.2.11 on 2026-10-27 23:53

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


def seed_audit_logs_permission(apps, schema_editor):
    from accounts.rbac_catalog import iter_permission_defs

    RbacPermission = apps.get_model('accounts', 'RbacPermission')
    for item in iter_permission_defs():
        RbacPermission.objects.update_or_create(
            codename=item['codename'],
            defaults={
                'module': item['module'],
                'module_label': item['module_label'],
                'action': item['action'],
                'label': item['label'],
                'sort_order': item['sort_order'],
            },
        )


class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0034_update_rbac_permission_labels'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='AuditLog',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('actor_email', models.CharField(blank=True, default='', max_length=254)),
                ('actor_username', models.CharField(blank=True, default='', max_length=150)),
                ('actor_role', models.CharField(choices=[('SUPERADMIN', 'Superadmin'), ('ADMIN', 'Admin'), ('USER', 'User'), ('OTHER', 'Other')], db_index=True, default='OTHER', max_length=20)),
                ('action', models.CharField(choices=[('CREATE', 'Create'), ('UPDATE', 'Update'), ('DELETE', 'Delete'), ('APPROVE', 'Approve'), ('VERIFY', 'Verify'), ('OVERRIDE', 'Override'), ('EXPORT', 'Export'), ('VIEW', 'View')], db_index=True, max_length=20)),
                ('module', models.CharField(db_index=True, max_length=64)),
                ('object_type', models.CharField(blank=True, default='', max_length=64)),
                ('object_id', models.CharField(blank=True, default='', max_length=64)),
                ('object_repr', models.CharField(blank=True, default='', max_length=255)),
                ('summary', models.TextField()),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('user_agent', models.CharField(blank=True, default='', max_length=512)),
                ('path', models.CharField(blank=True, default='', max_length=512)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('actor', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='audit_logs', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Audit log',
                'verbose_name_plural': 'Audit logs',
                'ordering': ['-created_at'],
                'indexes': [models.Index(fields=['module', 'action', '-created_at'], name='accounts_au_module_65ce37_idx'), models.Index(fields=['actor_email', '-created_at'], name='accounts_au_actor_e_57037a_idx')],
            },
        ),
        migrations.RunPython(seed_audit_logs_permission, migrations.RunPython.noop),
    ]
