# Generated by Django 5.2.7 on 2025-11-03 03:08

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


class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
        ('users', '0003_seed_granular_permissions'),
    ]

    operations = [
        migrations.CreateModel(
            name='DataAccessPattern',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('date', models.DateField()),
                ('total_requests', models.IntegerField(default=0)),
                ('unique_pages_accessed', models.IntegerField(default=0)),
                ('data_exports', models.IntegerField(default=0)),
                ('failed_access_attempts', models.IntegerField(default=0)),
                ('first_access', models.TimeField(blank=True, null=True)),
                ('last_access', models.TimeField(blank=True, null=True)),
                ('after_hours_access', models.IntegerField(default=0)),
                ('unique_ip_addresses', models.IntegerField(default=0)),
                ('primary_ip', models.GenericIPAddressField(blank=True, null=True)),
                ('is_anomalous', models.BooleanField(default=False)),
                ('anomaly_score', models.FloatField(default=0.0)),
                ('anomaly_reasons', models.JSONField(blank=True, default=list)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='access_patterns', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'data_access_patterns',
                'ordering': ['-date'],
                'indexes': [models.Index(fields=['user', 'date'], name='data_access_user_id_19ea69_idx'), models.Index(fields=['is_anomalous', 'date'], name='data_access_is_anom_e607a1_idx'), models.Index(fields=['anomaly_score', 'date'], name='data_access_anomaly_a45acd_idx')],
                'unique_together': {('user', 'date')},
            },
        ),
        migrations.CreateModel(
            name='EnhancedAuditLog',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('event_type', models.CharField(choices=[('login', 'Login'), ('logout', 'Logout'), ('permission_change', 'Permission Change'), ('data_access', 'Data Access'), ('data_modification', 'Data Modification'), ('security_violation', 'Security Violation'), ('system_config', 'System Configuration'), ('export_data', 'Data Export'), ('report_generation', 'Report Generation'), ('failed_access', 'Failed Access Attempt'), ('bulk_operation', 'Bulk Operation')], max_length=50)),
                ('action', models.CharField(max_length=100)),
                ('module', models.CharField(max_length=50)),
                ('permission_checked', models.CharField(blank=True, max_length=100, null=True)),
                ('permission_granted', models.BooleanField(blank=True, null=True)),
                ('role_at_time', models.CharField(blank=True, max_length=50, null=True)),
                ('branch_context', models.CharField(blank=True, max_length=100, null=True)),
                ('object_id', models.PositiveIntegerField(blank=True, null=True)),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('user_agent', models.CharField(blank=True, max_length=500, null=True)),
                ('session_id', models.CharField(blank=True, max_length=100, null=True)),
                ('request_method', models.CharField(blank=True, max_length=10, null=True)),
                ('request_path', models.CharField(blank=True, max_length=500, null=True)),
                ('request_params', models.JSONField(blank=True, default=dict)),
                ('status_code', models.IntegerField(blank=True, null=True)),
                ('response_time', models.FloatField(blank=True, null=True)),
                ('description', models.TextField()),
                ('additional_data', models.JSONField(blank=True, default=dict)),
                ('is_security_event', models.BooleanField(default=False)),
                ('severity', models.CharField(blank=True, choices=[('low', 'Low'), ('medium', 'Medium'), ('high', 'High'), ('critical', 'Critical')], max_length=20, null=True)),
                ('requires_attention', models.BooleanField(default=False)),
                ('timestamp', models.DateTimeField(auto_now_add=True)),
                ('content_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')),
                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='enhanced_audit_logs', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'enhanced_audit_logs',
                'ordering': ['-timestamp'],
                'indexes': [models.Index(fields=['user', 'timestamp'], name='enhanced_au_user_id_bbae48_idx'), models.Index(fields=['event_type', 'timestamp'], name='enhanced_au_event_t_45f2dc_idx'), models.Index(fields=['module', 'timestamp'], name='enhanced_au_module_b46c53_idx'), models.Index(fields=['permission_checked', 'timestamp'], name='enhanced_au_permiss_8e950e_idx'), models.Index(fields=['is_security_event', 'timestamp'], name='enhanced_au_is_secu_384b02_idx'), models.Index(fields=['severity', 'timestamp'], name='enhanced_au_severit_fa054b_idx'), models.Index(fields=['ip_address', 'timestamp'], name='enhanced_au_ip_addr_eb3927_idx')],
            },
        ),
        migrations.CreateModel(
            name='PermissionChangeLog',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('permission_type', models.CharField(max_length=50)),
                ('permission_name', models.CharField(max_length=100)),
                ('old_value', models.BooleanField(blank=True, null=True)),
                ('new_value', models.BooleanField(blank=True, null=True)),
                ('reason', models.TextField(blank=True)),
                ('approval_required', models.BooleanField(default=False)),
                ('approved_at', models.DateTimeField(blank=True, null=True)),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('session_id', models.CharField(blank=True, max_length=100, null=True)),
                ('timestamp', models.DateTimeField(auto_now_add=True)),
                ('approved_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='permission_approvals', to=settings.AUTH_USER_MODEL)),
                ('changed_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='permission_changes_made', to=settings.AUTH_USER_MODEL)),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='permission_changes', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'permission_change_logs',
                'ordering': ['-timestamp'],
                'indexes': [models.Index(fields=['user', 'timestamp'], name='permission__user_id_1ac2cd_idx'), models.Index(fields=['changed_by', 'timestamp'], name='permission__changed_752e2b_idx'), models.Index(fields=['permission_name', 'timestamp'], name='permission__permiss_ad48e7_idx')],
            },
        ),
        migrations.CreateModel(
            name='SecurityAlert',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('alert_type', models.CharField(choices=[('multiple_failed_logins', 'Multiple Failed Logins'), ('permission_escalation', 'Permission Escalation Attempt'), ('unusual_access_pattern', 'Unusual Access Pattern'), ('bulk_data_access', 'Bulk Data Access'), ('after_hours_access', 'After Hours Access'), ('suspicious_ip', 'Suspicious IP Address'), ('role_change', 'Role Change'), ('critical_permission_grant', 'Critical Permission Granted')], max_length=50)),
                ('severity', models.CharField(choices=[('low', 'Low'), ('medium', 'Medium'), ('high', 'High'), ('critical', 'Critical')], max_length=20)),
                ('title', models.CharField(max_length=200)),
                ('description', models.TextField()),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('user_agent', models.CharField(blank=True, max_length=500, null=True)),
                ('additional_context', models.JSONField(blank=True, default=dict)),
                ('is_resolved', models.BooleanField(default=False)),
                ('resolved_at', models.DateTimeField(blank=True, null=True)),
                ('resolution_notes', models.TextField(blank=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('resolved_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='resolved_alerts', to=settings.AUTH_USER_MODEL)),
                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='security_alerts', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'security_alerts',
                'ordering': ['-created_at'],
                'indexes': [models.Index(fields=['alert_type', 'created_at'], name='security_al_alert_t_3110ff_idx'), models.Index(fields=['severity', 'created_at'], name='security_al_severit_dcf72b_idx'), models.Index(fields=['user', 'created_at'], name='security_al_user_id_917922_idx'), models.Index(fields=['is_resolved', 'created_at'], name='security_al_is_reso_cbc72d_idx')],
            },
        ),
    ]
