# 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 = [
        ('journeys', '0001_initial'),
        ('organizations', '0001_initial'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='SurveyTemplate',
            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)),
                ('name_ar', models.CharField(blank=True, max_length=200, verbose_name='Name (Arabic)')),
                ('survey_type', models.CharField(choices=[('stage', 'Journey Stage Survey'), ('complaint_resolution', 'Complaint Resolution Satisfaction'), ('general', 'General Feedback'), ('nps', 'Net Promoter Score')], db_index=True, default='stage', max_length=50)),
                ('scoring_method', models.CharField(choices=[('average', 'Average Score'), ('weighted', 'Weighted Average'), ('nps', 'NPS Calculation')], default='average', max_length=20)),
                ('negative_threshold', models.DecimalField(decimal_places=1, default=3.0, help_text='Scores below this trigger PX actions (out of 5)', max_digits=3)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('instructions_en', models.TextField(blank=True, verbose_name='Instructions (English)')),
                ('instructions_ar', models.TextField(blank=True, verbose_name='Instructions (Arabic)')),
                ('consent_text_en', models.TextField(blank=True, verbose_name='Consent Text (English)')),
                ('consent_text_ar', models.TextField(blank=True, verbose_name='Consent Text (Arabic)')),
                ('requires_consent', models.BooleanField(default=True, verbose_name='Require patient consent')),
                ('hospital', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='survey_templates', to='organizations.hospital')),
            ],
            options={
                'ordering': ['hospital', 'name'],
            },
        ),
        migrations.CreateModel(
            name='SurveyQuestion',
            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)),
                ('text', models.TextField(verbose_name='Question Text (English)')),
                ('text_ar', models.TextField(blank=True, verbose_name='Question Text (Arabic)')),
                ('question_type', models.CharField(choices=[('rating', 'Rating (1-5 stars)'), ('nps', 'NPS (0-10)'), ('yes_no', 'Yes/No'), ('multiple_choice', 'Multiple Choice'), ('text', 'Text (Short Answer)'), ('textarea', 'Text Area (Long Answer)'), ('likert', 'Likert Scale (1-5)')], default='rating', max_length=20)),
                ('order', models.IntegerField(default=0, help_text='Display order')),
                ('is_required', models.BooleanField(default=True)),
                ('choices_json', models.JSONField(blank=True, default=list, help_text='Array of choice objects')),
                ('is_base', models.BooleanField(db_index=True, default=False, help_text='Always include this question regardless of events')),
                ('event_type', models.CharField(blank=True, db_index=True, help_text="HIS event type that triggers this question (e.g., 'Lab Bill', 'Triage')", max_length=200)),
                ('is_conditional', models.BooleanField(db_index=True, default=False, help_text='If True, this question is hidden by default and only shown via routing rules')),
                ('survey_template', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='questions', to='surveys.surveytemplate')),
            ],
            options={
                'ordering': ['survey_template', 'order'],
            },
        ),
        migrations.CreateModel(
            name='SurveyInstance',
            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)),
                ('encounter_id', models.CharField(blank=True, db_index=True, max_length=100)),
                ('delivery_channel', models.CharField(choices=[('sms', 'SMS'), ('whatsapp', 'WhatsApp'), ('email', 'Email')], default='sms', max_length=20)),
                ('recipient_phone', models.CharField(blank=True, max_length=20, null=True)),
                ('recipient_email', models.EmailField(blank=True, max_length=254, null=True)),
                ('access_token', models.CharField(blank=True, help_text='Secure token for survey access', max_length=100, unique=True)),
                ('token_expires_at', models.DateTimeField(blank=True, help_text='Token expiration date', null=True)),
                ('status', models.CharField(choices=[('pending', 'Pending (Scheduled, Not Yet Sent)'), ('sent', 'Sent (Not Opened)'), ('viewed', 'Viewed (Opened, Not Started)'), ('in_progress', 'In Progress (Started, Not Completed)'), ('completed', 'Completed'), ('abandoned', 'Abandoned (Started but Left)'), ('expired', 'Expired'), ('cancelled', 'Cancelled'), ('failed', 'Failed')], db_index=True, default='sent', max_length=20)),
                ('sent_at', models.DateTimeField(blank=True, db_index=True, null=True)),
                ('scheduled_send_at', models.DateTimeField(blank=True, db_index=True, help_text='When this survey should be sent (for delayed sending)', null=True)),
                ('opened_at', models.DateTimeField(blank=True, null=True)),
                ('completed_at', models.DateTimeField(blank=True, null=True)),
                ('open_count', models.IntegerField(default=0, help_text='Number of times survey link was opened')),
                ('last_opened_at', models.DateTimeField(blank=True, help_text='Most recent time survey was opened', null=True)),
                ('time_spent_seconds', models.IntegerField(blank=True, help_text='Total time spent on survey in seconds', null=True)),
                ('total_score', models.DecimalField(blank=True, decimal_places=2, help_text='Calculated total score', max_digits=5, null=True)),
                ('is_negative', models.BooleanField(db_index=True, default=False, help_text='True if score below threshold')),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('patient_contacted', models.BooleanField(default=False, help_text='Whether patient was contacted about negative survey')),
                ('patient_contacted_at', models.DateTimeField(blank=True, null=True)),
                ('contact_notes', models.TextField(blank=True, help_text='Notes from patient contact')),
                ('issue_resolved', models.BooleanField(default=False, help_text='Whether the issue was resolved/explained')),
                ('comment', models.TextField(blank=True, help_text='Optional comment from patient')),
                ('comment_analyzed', models.BooleanField(default=False, help_text='Whether the comment has been analyzed by AI')),
                ('comment_analysis', models.JSONField(blank=True, default=dict, help_text='AI analysis results for the comment')),
                ('completed_language', models.CharField(blank=True, help_text='Language selected by patient when completing survey (en/ar)', max_length=10)),
                ('hospital', models.ForeignKey(help_text='Tenant hospital for this record', on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_related', to='organizations.hospital')),
                ('journey_instance', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='surveys', to='journeys.patientjourneyinstance')),
                ('patient', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='surveys', to='organizations.patient')),
                ('patient_contacted_by', models.ForeignKey(blank=True, help_text='User who contacted the patient', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='contacted_surveys', to=settings.AUTH_USER_MODEL)),
                ('staff', models.ForeignKey(blank=True, help_text='Staff recipient (if survey is for staff)', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='surveys', to='organizations.staff')),
                ('survey_template', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='instances', to='surveys.surveytemplate')),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='QuestionRoutingRule',
            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)),
                ('operator', models.CharField(choices=[('equals', 'Equals'), ('not_equals', 'Not Equals'), ('contains', 'Contains'), ('gt', 'Greater Than'), ('lt', 'Less Than'), ('in_list', 'In List'), ('answered', 'Is Answered'), ('not_answered', 'Is Not Answered')], max_length=20)),
                ('value', models.JSONField(blank=True, default=dict, help_text="Comparison value (e.g., 'yes', 3, ['a','b']). Not needed for answered/not_answered.")),
                ('action', models.CharField(choices=[('skip_to', 'Skip To Question'), ('end_survey', 'End Survey')], max_length=20)),
                ('order', models.IntegerField(default=0, help_text='Evaluation order when multiple rules exist for the same source question')),
                ('source_question', models.ForeignKey(help_text='The question whose answer triggers this rule', on_delete=django.db.models.deletion.CASCADE, related_name='outgoing_routing_rules', to='surveys.surveyquestion')),
                ('target_question', models.ForeignKey(blank=True, help_text='Target question for skip_to action. Not needed for end_survey.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='incoming_routing_rules', to='surveys.surveyquestion')),
                ('survey_template', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='routing_rules', to='surveys.surveytemplate')),
            ],
            options={
                'ordering': ['source_question', 'order'],
            },
        ),
        migrations.CreateModel(
            name='BulkSurveyJob',
            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(blank=True, max_length=200)),
                ('status', models.CharField(choices=[('pending', 'Pending'), ('processing', 'Processing'), ('completed', 'Completed'), ('failed', 'Failed'), ('partial', 'Partially Completed')], db_index=True, default='pending', max_length=20)),
                ('source', models.CharField(choices=[('his_import', 'HIS Import'), ('csv_upload', 'CSV Upload'), ('manual', 'Manual')], default='manual', max_length=20)),
                ('total_patients', models.IntegerField(default=0)),
                ('processed_count', models.IntegerField(default=0)),
                ('success_count', models.IntegerField(default=0)),
                ('failed_count', models.IntegerField(default=0)),
                ('delivery_channel', models.CharField(default='sms', max_length=20)),
                ('custom_message', models.TextField(blank=True)),
                ('patient_data', models.JSONField(default=list, help_text='List of patient IDs and file numbers to process')),
                ('results', models.JSONField(blank=True, default=dict, help_text='Detailed results including successes and failures')),
                ('error_message', models.TextField(blank=True)),
                ('started_at', models.DateTimeField(blank=True, null=True)),
                ('completed_at', models.DateTimeField(blank=True, null=True)),
                ('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='bulk_survey_jobs', to=settings.AUTH_USER_MODEL)),
                ('hospital', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bulk_survey_jobs', to='organizations.hospital')),
                ('survey_template', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='bulk_jobs', to='surveys.surveytemplate')),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='SurveyTracking',
            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)),
                ('event_type', models.CharField(choices=[('page_view', 'Page View'), ('survey_started', 'Survey Started'), ('question_answered', 'Question Answered'), ('survey_abandoned', 'Survey Abandoned'), ('survey_completed', 'Survey Completed'), ('reminder_sent', 'Reminder Sent')], db_index=True, max_length=50)),
                ('time_on_page', models.IntegerField(blank=True, help_text='Time spent on page in seconds', null=True)),
                ('total_time_spent', models.IntegerField(blank=True, help_text='Total time spent on survey so far in seconds', null=True)),
                ('current_question', models.IntegerField(blank=True, help_text='Question number when event occurred', null=True)),
                ('user_agent', models.TextField(blank=True)),
                ('ip_address', models.GenericIPAddressField(blank=True, null=True)),
                ('device_type', models.CharField(blank=True, help_text='mobile, tablet, desktop', max_length=50)),
                ('browser', models.CharField(blank=True, max_length=100)),
                ('country', models.CharField(blank=True, max_length=100)),
                ('city', models.CharField(blank=True, max_length=100)),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('survey_instance', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tracking_events', to='surveys.surveyinstance')),
            ],
            options={
                'ordering': ['survey_instance', 'created_at'],
            },
        ),
        migrations.CreateModel(
            name='SurveyResponse',
            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)),
                ('numeric_value', models.DecimalField(blank=True, decimal_places=2, help_text='For rating, NPS, Likert questions', max_digits=10, null=True)),
                ('text_value', models.TextField(blank=True, help_text='For text, textarea questions')),
                ('choice_value', models.CharField(blank=True, help_text='For multiple choice questions', max_length=200)),
                ('response_time_seconds', models.FloatField(blank=True, help_text='Time taken to answer this question in seconds', null=True)),
                ('analysis', models.JSONField(blank=True, default=dict, help_text='AI analysis results for text responses')),
                ('question', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='responses', to='surveys.surveyquestion')),
                ('survey_instance', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='responses', to='surveys.surveyinstance')),
            ],
            options={
                'ordering': ['survey_instance', 'question__order'],
                'unique_together': {('survey_instance', 'question')},
            },
        ),
        migrations.AddIndex(
            model_name='surveytemplate',
            index=models.Index(fields=['hospital', 'survey_type', 'is_active'], name='surveys_sur_hospita_0c8e30_idx'),
        ),
        migrations.AddIndex(
            model_name='surveyinstance',
            index=models.Index(fields=['patient', '-created_at'], name='surveys_sur_patient_7e68b1_idx'),
        ),
        migrations.AddIndex(
            model_name='surveyinstance',
            index=models.Index(fields=['staff', '-created_at'], name='surveys_sur_staff_i_1230b7_idx'),
        ),
        migrations.AddIndex(
            model_name='surveyinstance',
            index=models.Index(fields=['status', '-sent_at'], name='surveys_sur_status_ce377b_idx'),
        ),
        migrations.AddIndex(
            model_name='surveyinstance',
            index=models.Index(fields=['is_negative', '-completed_at'], name='surveys_sur_is_nega_46c933_idx'),
        ),
        migrations.AddIndex(
            model_name='surveyinstance',
            index=models.Index(fields=['survey_template', 'status', '-created_at'], name='surveys_sur_survey__5b13b3_idx'),
        ),
        migrations.AddIndex(
            model_name='questionroutingrule',
            index=models.Index(fields=['survey_template', 'source_question', 'order'], name='surveys_que_survey__2afb9a_idx'),
        ),
        migrations.AddIndex(
            model_name='bulksurveyjob',
            index=models.Index(fields=['status', '-created_at'], name='surveys_bul_status_e13dfa_idx'),
        ),
        migrations.AddIndex(
            model_name='bulksurveyjob',
            index=models.Index(fields=['created_by', '-created_at'], name='surveys_bul_created_d83e06_idx'),
        ),
        migrations.AddIndex(
            model_name='bulksurveyjob',
            index=models.Index(fields=['hospital', '-created_at'], name='surveys_bul_hospita_0357db_idx'),
        ),
        migrations.AddIndex(
            model_name='surveytracking',
            index=models.Index(fields=['survey_instance', 'event_type', '-created_at'], name='surveys_sur_survey__9743a1_idx'),
        ),
        migrations.AddIndex(
            model_name='surveytracking',
            index=models.Index(fields=['event_type', '-created_at'], name='surveys_sur_event_t_885d23_idx'),
        ),
    ]
