# Generated by Django 5.2.19 on 2026-12-01 16:25

import apps.notifications.models
import django.core.serializers.json
import django.db.models.deletion
import django.utils.timezone
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('accounting', '0010_voucherregisterviewconfig_and_more'),
        ('tenancy', '0003_taxconfig_calculation_rule_taxconfig_category_and_more'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ConfirmationAudit',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('action_type', models.CharField(db_index=True, max_length=64)),
                ('source_object_type', models.CharField(blank=True, max_length=64)),
                ('source_object_id', models.CharField(blank=True, max_length=64)),
                ('source_reference', models.CharField(blank=True, max_length=128)),
                ('confirmation_phrase_used', models.CharField(blank=True, max_length=128)),
                ('confirmation_timestamp', models.DateTimeField(default=django.utils.timezone.now)),
                ('authorization_result', models.CharField(default='ALLOWED', max_length=24)),
                ('execution_result', models.CharField(default='SUCCESS', max_length=24)),
                ('detail', models.TextField(blank=True)),
                ('actor', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
                ('company', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='confirmation_audits', to='tenancy.company')),
                ('second_approver', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='second_approved_confirmations', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'notif_confirmation_audits',
                'ordering': ['-confirmation_timestamp'],
            },
        ),
        migrations.CreateModel(
            name='Notification',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('event_type', models.CharField(db_index=True, max_length=96)),
                ('title', models.CharField(max_length=180)),
                ('message', models.TextField()),
                ('description', models.TextField(blank=True)),
                ('priority', models.CharField(choices=[('CRITICAL', 'Critical'), ('HIGH', 'High'), ('NORMAL', 'Normal'), ('LOW', 'Low')], db_index=True, default='NORMAL', max_length=16)),
                ('category', models.CharField(choices=[('GENERAL', 'General'), ('MESSAGE', 'Chat & Messages'), ('APPROVAL', 'Tasks & Approvals'), ('SYSTEM', 'System & Updates'), ('SECURITY', 'Security & Audit'), ('BACKUP', 'Backup & Recovery'), ('ACCOUNTING', 'Accounting & Vouchers'), ('BANKING', 'Banking & Treasury'), ('PARTY_HUB', 'AP / AR'), ('PROJECT', 'Projects & Sites'), ('HR', 'HR & Payroll'), ('REPORTS', 'Reports')], db_index=True, default='GENERAL', max_length=24)),
                ('module', models.CharField(db_index=True, default='SYSTEM', max_length=48)),
                ('popup_kind', models.CharField(default='INFO', max_length=24)),
                ('source_object_type', models.CharField(blank=True, max_length=64)),
                ('source_object_id', models.CharField(blank=True, max_length=64)),
                ('source_reference', models.CharField(blank=True, max_length=128)),
                ('route_key', models.CharField(blank=True, max_length=128)),
                ('action_definition', models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder)),
                ('metadata', models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder)),
                ('dedup_key', models.CharField(blank=True, db_index=True, max_length=191, null=True)),
                ('is_mandatory_critical', models.BooleanField(default=False)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('expires_at', models.DateTimeField(blank=True, null=True)),
                ('status', models.CharField(choices=[('ACTIVE', 'Active'), ('STALE', 'Stale (Source Updated)'), ('EXECUTED', 'Action Executed'), ('EXPIRED', 'Expired')], db_index=True, default='ACTIVE', max_length=16)),
                ('branch', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='tenancy.branch')),
                ('company', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to='tenancy.company')),
                ('department', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounting.department')),
                ('recipient', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to=settings.AUTH_USER_MODEL)),
                ('sender', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='sent_notifications', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'notif_notifications',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='NotificationEvent',
            fields=[
                ('event_id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('event_type', models.CharField(db_index=True, max_length=96)),
                ('source_object_type', models.CharField(blank=True, max_length=64)),
                ('source_object_id', models.CharField(blank=True, max_length=64)),
                ('source_reference', models.CharField(blank=True, max_length=128)),
                ('dedup_key', models.CharField(blank=True, db_index=True, max_length=191)),
                ('payload', models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('processed_at', models.DateTimeField(blank=True, null=True)),
                ('processing_status', models.CharField(choices=[('PENDING', 'Pending'), ('PROCESSED', 'Processed'), ('FAILED', 'Failed')], db_index=True, default='PENDING', max_length=16)),
                ('error_detail', models.CharField(blank=True, max_length=255)),
                ('actor', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='triggered_notification_events', to=settings.AUTH_USER_MODEL)),
                ('company', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='notification_events', to='tenancy.company')),
            ],
            options={
                'db_table': 'notif_events',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='NotificationDelivery',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('delivery_channel', models.CharField(default='IN_APP', max_length=24)),
                ('delivery_status', models.CharField(default='DELIVERED', max_length=24)),
                ('attempt_count', models.PositiveSmallIntegerField(default=1)),
                ('delivered_at', models.DateTimeField(blank=True, null=True)),
                ('error_reference', models.CharField(blank=True, max_length=255)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('notification', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='deliveries', to='notifications.notification')),
                ('recipient', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
                ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='deliveries', to='notifications.notificationevent')),
            ],
            options={
                'db_table': 'notif_deliveries',
                'ordering': ['-created_at'],
            },
        ),
        migrations.AddField(
            model_name='notification',
            name='event',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='notifications', to='notifications.notificationevent'),
        ),
        migrations.CreateModel(
            name='NotificationGlobalConfiguration',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('retention_days', models.PositiveIntegerField(default=180)),
                ('critical_escalation_mandatory', models.BooleanField(default=True)),
                ('mandatory_security_popup', models.BooleanField(default=True)),
                ('rate_limit_per_minute', models.PositiveIntegerField(default=120)),
                ('quiet_hours_enforced_for_non_critical', models.BooleanField(default=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('company', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='notification_config', to='tenancy.company')),
            ],
            options={
                'db_table': 'notif_global_configurations',
            },
        ),
        migrations.CreateModel(
            name='NotificationPreference',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('category', models.CharField(default='ALL', max_length=32)),
                ('popup_enabled', models.BooleanField(default=True)),
                ('center_screen_enabled', models.BooleanField(default=True)),
                ('sound_enabled', models.BooleanField(default=True)),
                ('desktop_enabled', models.BooleanField(default=False)),
                ('grouping_enabled', models.BooleanField(default=True)),
                ('badge_enabled', models.BooleanField(default=True)),
                ('history_enabled', models.BooleanField(default=True)),
                ('message_preview', models.BooleanField(default=True)),
                ('delivery_mode', models.CharField(default='IMMEDIATE', max_length=24)),
                ('popup_size', models.CharField(default='STANDARD', max_length=16)),
                ('popup_position', models.CharField(default='CENTER', max_length=16)),
                ('theme_mode', models.CharField(default='FOLLOW_APP', max_length=16)),
                ('animation_enabled', models.BooleanField(default=True)),
                ('reduced_motion', models.BooleanField(default=False)),
                ('max_simultaneous', models.PositiveSmallIntegerField(default=3)),
                ('auto_dismiss_ms', models.JSONField(blank=True, default=apps.notifications.models.default_auto_dismiss_ms)),
                ('quiet_hours_start', models.TimeField(blank=True, null=True)),
                ('quiet_hours_end', models.TimeField(blank=True, null=True)),
                ('timezone_name', models.CharField(default='Asia/Kathmandu', max_length=64)),
                ('allow_critical_in_quiet', models.BooleanField(default=True)),
                ('dnd_enabled', models.BooleanField(default=False)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notification_preferences', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'notif_preferences',
            },
        ),
        migrations.CreateModel(
            name='NotificationRecipientState',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('is_read', models.BooleanField(db_index=True, default=False)),
                ('read_at', models.DateTimeField(blank=True, null=True)),
                ('is_archived', models.BooleanField(db_index=True, default=False)),
                ('archived_at', models.DateTimeField(blank=True, null=True)),
                ('dismissed_at', models.DateTimeField(blank=True, null=True)),
                ('acknowledged_at', models.DateTimeField(blank=True, null=True)),
                ('executed_action', models.CharField(blank=True, max_length=64)),
                ('executed_at', models.DateTimeField(blank=True, null=True)),
                ('notification', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='state', to='notifications.notification')),
                ('recipient', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notification_states', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'notif_recipient_states',
            },
        ),
        migrations.AddIndex(
            model_name='notificationevent',
            index=models.Index(fields=['company', 'event_type', 'created_at'], name='notif_event_company_5ec165_idx'),
        ),
        migrations.AddIndex(
            model_name='notification',
            index=models.Index(fields=['recipient', 'company', 'created_at'], name='notif_notif_recipie_5192ea_idx'),
        ),
        migrations.AddIndex(
            model_name='notification',
            index=models.Index(fields=['recipient', 'category', 'status'], name='notif_notif_recipie_a99122_idx'),
        ),
        migrations.AddIndex(
            model_name='notification',
            index=models.Index(fields=['source_object_type', 'source_object_id'], name='notif_notif_source__c98e93_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='notification',
            unique_together={('company', 'recipient', 'dedup_key')},
        ),
        migrations.AlterUniqueTogether(
            name='notificationpreference',
            unique_together={('user', 'category')},
        ),
        migrations.AddIndex(
            model_name='notificationrecipientstate',
            index=models.Index(fields=['recipient', 'is_read', 'is_archived'], name='notif_recip_recipie_e59acd_idx'),
        ),
    ]
