# Generated by Django 6.0.1 on 2026-05-11 20:32

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('organizations', '0001_initial'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='KPI',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=200, unique=True)),
                ('name_ar', models.CharField(blank=True, max_length=200)),
                ('description', models.TextField(blank=True)),
                ('category', models.CharField(choices=[('patient_satisfaction', 'Patient Satisfaction'), ('complaint_management', 'Complaint Management'), ('action_management', 'Action Management'), ('sla_compliance', 'SLA Compliance'), ('survey_response', 'Survey Response'), ('operational', 'Operational')], db_index=True, max_length=100)),
                ('unit', models.CharField(help_text='Unit of measurement (%, count, hours, etc.)', max_length=50)),
                ('calculation_method', models.TextField(help_text='Description of how this KPI is calculated')),
                ('target_value', models.DecimalField(blank=True, decimal_places=2, help_text='Target value for this KPI', max_digits=10, null=True)),
                ('warning_threshold', models.DecimalField(blank=True, decimal_places=2, help_text='Warning threshold', max_digits=10, null=True)),
                ('critical_threshold', models.DecimalField(blank=True, decimal_places=2, help_text='Critical threshold', max_digits=10, null=True)),
                ('is_active', models.BooleanField(default=True)),
            ],
            options={
                'verbose_name': 'KPI',
                'verbose_name_plural': 'KPIs',
                'ordering': ['category', 'name'],
            },
        ),
        migrations.CreateModel(
            name='KPIReport',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('report_type', models.CharField(choices=[('resolution_72h', 'MOH-2: 72-Hour Resolution Rate'), ('patient_experience', 'MOH-1: Patient Experience Score'), ('satisfaction_resolution', 'MOH-3: Overall Satisfaction with Resolution'), ('n_pad_001', 'N-PAD-001: Resolution to Patient Complaints'), ('response_rate', 'Dep-KPI-4: Department Response Rate'), ('activation_2h', 'KPI-6: Complaint Activation Within 2 Hours'), ('unactivated', 'KPI-7: Unactivated Filled Complaints Rate'), ('moh_24h', 'MOH-24h: 24-Hour MOH Complaint Resolution Rate'), ('chi_48h', 'CHI-48h: 48-Hour CHI Complaint Resolution Rate')], db_index=True, help_text='Type of KPI report', max_length=50)),
                ('year', models.IntegerField(db_index=True)),
                ('month', models.IntegerField(db_index=True)),
                ('report_date', models.DateField(help_text='Date the report was generated')),
                ('status', models.CharField(choices=[('pending', 'Pending'), ('generating', 'Generating'), ('completed', 'Completed'), ('failed', 'Failed')], db_index=True, default='pending', max_length=20)),
                ('generated_at', models.DateTimeField(blank=True, null=True)),
                ('target_percentage', models.DecimalField(decimal_places=2, default=95.0, help_text='Target percentage for this KPI', max_digits=5)),
                ('threshold_percentage', models.DecimalField(decimal_places=2, default=90.0, help_text='Threshold (minimum acceptable) percentage for this KPI', max_digits=5)),
                ('category', models.CharField(default='Organizational', help_text='Report category (e.g., Organizational, Clinical)', max_length=50)),
                ('kpi_type', models.CharField(default='Outcome', help_text='KPI type (e.g., Outcome, Process, Structure)', max_length=50)),
                ('risk_level', models.CharField(choices=[('High', 'High'), ('Medium', 'Medium'), ('Low', 'Low')], default='High', help_text='Risk level for this KPI', max_length=20)),
                ('data_collection_method', models.CharField(default='Retrospective', help_text='Data collection method', max_length=50)),
                ('data_collection_frequency', models.CharField(default='Monthly', help_text='How often data is collected', max_length=50)),
                ('reporting_frequency', models.CharField(default='Monthly', help_text='How often report is generated', max_length=50)),
                ('dimension', models.CharField(default='Efficiency', help_text='KPI dimension (e.g., Efficiency, Quality, Safety)', max_length=50)),
                ('collector_name', models.CharField(blank=True, help_text='Name of data collector', max_length=200)),
                ('analyzer_name', models.CharField(blank=True, help_text='Name of data analyzer', max_length=200)),
                ('total_numerator', models.IntegerField(default=0, help_text='Total count of successful outcomes')),
                ('total_denominator', models.IntegerField(default=0, help_text='Total count of all cases')),
                ('overall_result', models.DecimalField(decimal_places=2, default=0.0, help_text='Overall percentage result', max_digits=6)),
                ('error_message', models.TextField(blank=True)),
                ('ai_analysis', models.JSONField(blank=True, help_text='AI-generated analysis and recommendations for this report', null=True)),
                ('ai_analysis_generated_at', models.DateTimeField(blank=True, help_text='When the AI analysis was generated', null=True)),
                ('generated_by', models.ForeignKey(blank=True, help_text='User who generated the report (null for automated)', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='generated_kpi_reports', to=settings.AUTH_USER_MODEL)),
                ('hospital', models.ForeignKey(help_text='Hospital this report belongs to', on_delete=django.db.models.deletion.CASCADE, related_name='kpi_reports', to='organizations.hospital')),
            ],
            options={
                'verbose_name': 'KPI Report',
                'verbose_name_plural': 'KPI Reports',
                'ordering': ['-year', '-month', 'report_type'],
            },
        ),
        migrations.CreateModel(
            name='KPIReportDepartmentBreakdown',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('department_category', models.CharField(choices=[('medical', 'Medical Department'), ('nursing', 'Nursing Department'), ('non_medical', 'Non-Medical / Admin'), ('support_services', 'Support Services')], help_text='Category of department', max_length=50)),
                ('complaint_count', models.IntegerField(default=0)),
                ('resolved_count', models.IntegerField(default=0)),
                ('avg_resolution_days', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)),
                ('top_areas', models.TextField(blank=True, help_text='Top complaint areas or notes (newline-separated)')),
                ('details', models.JSONField(blank=True, default=dict)),
                ('kpi_report', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='department_breakdowns', to='analytics.kpireport')),
            ],
            options={
                'verbose_name': 'KPI Department Breakdown',
                'verbose_name_plural': 'KPI Department Breakdowns',
                'ordering': ['department_category'],
            },
        ),
        migrations.CreateModel(
            name='KPIReportLocationBreakdown',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('location_type', models.CharField(choices=[('Inpatient', 'Inpatient'), ('Outpatient', 'Outpatient'), ('Emergency', 'Emergency')], help_text='Location type category', max_length=50)),
                ('complaint_count', models.IntegerField(default=0)),
                ('percentage', models.DecimalField(decimal_places=2, default=0.0, max_digits=5)),
                ('kpi_report', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='location_breakdowns', to='analytics.kpireport')),
            ],
            options={
                'verbose_name': 'KPI Location Breakdown',
                'verbose_name_plural': 'KPI Location Breakdowns',
                'ordering': ['location_type'],
            },
        ),
        migrations.CreateModel(
            name='KPIReportMonthlyData',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('month', models.IntegerField(db_index=True, help_text='Month number (1-12), 0 for TOTAL')),
                ('numerator', models.IntegerField(default=0, help_text='Count of successful outcomes')),
                ('denominator', models.IntegerField(default=0, help_text='Count of all cases')),
                ('percentage', models.DecimalField(decimal_places=2, default=0.0, help_text='Calculated percentage', max_digits=6)),
                ('is_below_target', models.BooleanField(default=False, help_text='Whether this month is below target')),
                ('details', models.JSONField(blank=True, default=dict, help_text='Additional breakdown data (e.g., by source, department)')),
                ('kpi_report', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='monthly_data', to='analytics.kpireport')),
            ],
            options={
                'verbose_name': 'KPI Monthly Data',
                'verbose_name_plural': 'KPI Monthly Data',
                'ordering': ['month'],
            },
        ),
        migrations.CreateModel(
            name='KPIReportSourceBreakdown',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('source_name', models.CharField(max_length=100)),
                ('complaint_count', models.IntegerField(default=0)),
                ('percentage', models.DecimalField(decimal_places=2, default=0.0, max_digits=5)),
                ('kpi_report', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='source_breakdowns', to='analytics.kpireport')),
            ],
            options={
                'verbose_name': 'KPI Source Breakdown',
                'verbose_name_plural': 'KPI Source Breakdowns',
                'ordering': ['-complaint_count'],
            },
        ),
        migrations.CreateModel(
            name='KPIValue',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('value', models.DecimalField(decimal_places=2, max_digits=10)),
                ('period_start', models.DateTimeField(db_index=True)),
                ('period_end', models.DateTimeField(db_index=True)),
                ('period_type', models.CharField(choices=[('hourly', 'Hourly'), ('daily', 'Daily'), ('weekly', 'Weekly'), ('monthly', 'Monthly'), ('quarterly', 'Quarterly'), ('yearly', 'Yearly')], default='daily', max_length=20)),
                ('status', models.CharField(choices=[('on_target', 'On Target'), ('warning', 'Warning'), ('critical', 'Critical')], db_index=True, max_length=20)),
                ('metadata', models.JSONField(blank=True, default=dict, help_text='Additional calculation details')),
                ('department', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='kpi_values', to='organizations.department')),
                ('hospital', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='kpi_values', to='organizations.hospital')),
                ('kpi', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='values', to='analytics.kpi')),
            ],
            options={
                'ordering': ['-period_end'],
            },
        ),
        migrations.AddIndex(
            model_name='kpireport',
            index=models.Index(fields=['report_type', '-year', '-month'], name='analytics_k_report__c0826c_idx'),
        ),
        migrations.AddIndex(
            model_name='kpireport',
            index=models.Index(fields=['hospital', '-year', '-month'], name='analytics_k_hospita_ba868a_idx'),
        ),
        migrations.AddIndex(
            model_name='kpireport',
            index=models.Index(fields=['status', '-created_at'], name='analytics_k_status_8c6a24_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='kpireport',
            unique_together={('report_type', 'hospital', 'year', 'month')},
        ),
        migrations.AlterUniqueTogether(
            name='kpireportdepartmentbreakdown',
            unique_together={('kpi_report', 'department_category')},
        ),
        migrations.AlterUniqueTogether(
            name='kpireportlocationbreakdown',
            unique_together={('kpi_report', 'location_type')},
        ),
        migrations.AddIndex(
            model_name='kpireportmonthlydata',
            index=models.Index(fields=['kpi_report', 'month'], name='analytics_k_kpi_rep_0c4281_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='kpireportmonthlydata',
            unique_together={('kpi_report', 'month')},
        ),
        migrations.AddIndex(
            model_name='kpivalue',
            index=models.Index(fields=['kpi', '-period_end'], name='analytics_k_kpi_id_f9c38d_idx'),
        ),
        migrations.AddIndex(
            model_name='kpivalue',
            index=models.Index(fields=['hospital', 'kpi', '-period_end'], name='analytics_k_hospita_356dca_idx'),
        ),
        migrations.AddIndex(
            model_name='kpivalue',
            index=models.Index(fields=['hospital', 'department', '-period_end'], name='analytics_k_hospita_ba95bd_idx'),
        ),
    ]
