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

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('integrations', '0001_initial'),
        ('organizations', '0001_initial'),
        ('surveys', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='hispatientvisit',
            name='survey_instance',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='his_visit', to='surveys.surveyinstance'),
        ),
        migrations.AddIndex(
            model_name='histestpatient',
            index=models.Index(fields=['patient_type', 'admit_date'], name='integration_patient_d70ddd_idx'),
        ),
        migrations.AddIndex(
            model_name='histestpatient',
            index=models.Index(fields=['ssn', 'admit_date'], name='integration_ssn_53c08e_idx'),
        ),
        migrations.AddIndex(
            model_name='histestpatient',
            index=models.Index(fields=['mobile_no', 'admit_date'], name='integration_mobile__a8a578_idx'),
        ),
        migrations.AddConstraint(
            model_name='histestpatient',
            constraint=models.UniqueConstraint(fields=('admission_id',), name='unique_test_patient_admission'),
        ),
        migrations.AddIndex(
            model_name='histestvisit',
            index=models.Index(fields=['admission_id', 'visit_category'], name='integration_admissi_948163_idx'),
        ),
        migrations.AddIndex(
            model_name='histestvisit',
            index=models.Index(fields=['patient_id', 'visit_category'], name='integration_patient_6151c1_idx'),
        ),
        migrations.AddIndex(
            model_name='histestvisit',
            index=models.Index(fields=['admission_id', 'bill_date'], name='integration_admissi_23d141_idx'),
        ),
        migrations.AddField(
            model_name='hisvisitevent',
            name='visit',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='visit_events', to='integrations.hispatientvisit'),
        ),
        migrations.AddIndex(
            model_name='inboundevent',
            index=models.Index(fields=['status', '-received_at'], name='integration_status_f5244c_idx'),
        ),
        migrations.AddIndex(
            model_name='inboundevent',
            index=models.Index(fields=['encounter_id', 'event_code'], name='integration_encount_e7d795_idx'),
        ),
        migrations.AddIndex(
            model_name='inboundevent',
            index=models.Index(fields=['source_system', '-received_at'], name='integration_source__bacde5_idx'),
        ),
        migrations.AddField(
            model_name='eventmapping',
            name='integration_config',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='event_mappings', to='integrations.integrationconfig'),
        ),
        migrations.AddField(
            model_name='surveytemplatemapping',
            name='hospital',
            field=models.ForeignKey(blank=True, help_text='Hospital for this mapping (null = applies to all hospitals)', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='survey_template_mappings', to='organizations.hospital'),
        ),
        migrations.AddField(
            model_name='surveytemplatemapping',
            name='survey_template',
            field=models.ForeignKey(help_text='Survey template to send for this patient type', on_delete=django.db.models.deletion.CASCADE, related_name='patient_type_mappings', to='surveys.surveytemplate'),
        ),
        migrations.AddIndex(
            model_name='hispatientvisit',
            index=models.Index(fields=['patient_type', 'is_visit_complete'], name='integration_patient_89c76d_idx'),
        ),
        migrations.AddIndex(
            model_name='hispatientvisit',
            index=models.Index(fields=['patient_type', 'last_his_fetch_at'], name='integration_patient_1e82ed_idx'),
        ),
        migrations.AddIndex(
            model_name='hispatientvisit',
            index=models.Index(fields=['admission_id', 'is_visit_complete'], name='integration_admissi_a63b9a_idx'),
        ),
        migrations.AddIndex(
            model_name='hispatientvisit',
            index=models.Index(fields=['hospital', 'patient_type', 'is_visit_complete'], name='integration_hospita_1e17bb_idx'),
        ),
        migrations.AddConstraint(
            model_name='hispatientvisit',
            constraint=models.UniqueConstraint(fields=('admission_id',), name='unique_his_visit_per_admission'),
        ),
        migrations.AddIndex(
            model_name='hisvisitevent',
            index=models.Index(fields=['visit', 'parsed_date'], name='integration_visit_i_901d6c_idx'),
        ),
        migrations.AddIndex(
            model_name='hisvisitevent',
            index=models.Index(fields=['visit_category', 'parsed_date'], name='integration_visit_c_0a2be5_idx'),
        ),
        migrations.AlterUniqueTogether(
            name='eventmapping',
            unique_together={('integration_config', 'external_event_code')},
        ),
        migrations.AddIndex(
            model_name='surveytemplatemapping',
            index=models.Index(fields=['patient_type', 'hospital', 'is_active'], name='integration_patient_3e4ef8_idx'),
        ),
        migrations.AddConstraint(
            model_name='surveytemplatemapping',
            constraint=models.UniqueConstraint(condition=models.Q(('is_active', True)), fields=('patient_type', 'hospital'), name='unique_active_mapping_per_type_hospital'),
        ),
    ]
