# Generated by Django 5.2.17 on 2026-09-26 18:54

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 = [
        ('customers', '0002_customeraddress_customercontact'),
        ('devices', '0003_customer_device_relationship'),
        ('organization', '0002_userorganizationassignment'),
        ('service_catalog', '0002_technical_taxonomy'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ServiceCase',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('job_number', models.CharField(editable=False, max_length=22)),
                ('intake_channel', models.CharField(choices=[('WALK_IN', 'Walk-in'), ('COURIER', 'Courier'), ('PICKUP', 'Pickup'), ('DEALER', 'Dealer'), ('OTHER', 'Other')], max_length=8)),
                ('received_at', models.DateTimeField(default=django.utils.timezone.now)),
                ('status', models.CharField(choices=[('RECEIVED', 'Received'), ('CANCELLED', 'Cancelled')], default='RECEIVED', max_length=9)),
                ('customer_reference', models.CharField(blank=True, max_length=200)),
                ('intake_note', models.TextField(blank=True, help_text='Do not record passwords, screen PINs, account credentials or OTPs.', max_length=4000)),
                ('cancelled_at', models.DateTimeField(blank=True, editable=False, null=True)),
                ('cancellation_reason', models.CharField(blank=True, help_text='Do not record passwords, screen PINs, account credentials or OTPs.', max_length=2000)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='service_cases', to='organization.company')),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='created_service_cases', to=settings.AUTH_USER_MODEL)),
                ('customer', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='service_cases', to='customers.customer')),
                ('device', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='service_cases', to='devices.device')),
                ('service_center', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='service_cases', to='organization.servicecenter')),
            ],
            options={
                'ordering': ['-received_at', 'id'],
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='ServiceCaseAccessory',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('accessory_type', models.CharField(choices=[('CHARGER', 'Charger'), ('CABLE', 'Cable'), ('CASE', 'Case'), ('SIM', 'SIM'), ('MEMORY_CARD', 'Memory card'), ('BOX', 'Box'), ('OTHER', 'Other')], max_length=11)),
                ('description', models.TextField(blank=True, help_text='Do not record passwords, screen PINs, account credentials or OTPs.', max_length=2000)),
                ('quantity', models.PositiveIntegerField(default=1)),
                ('service_case', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='accessories', to='service.servicecase')),
            ],
            options={
                'ordering': ['created_at', 'id'],
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='ServiceCaseComplaint',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('customer_description', models.TextField(blank=True, help_text='Do not record passwords, screen PINs, account credentials or OTPs.', max_length=2000)),
                ('removed_at', models.DateTimeField(blank=True, editable=False, null=True)),
                ('complaint_symptom', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='service_case_complaints', to='service_catalog.complaintsymptom')),
                ('service_case', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='complaints', to='service.servicecase')),
            ],
            options={
                'ordering': ['created_at', 'id'],
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='ServiceCaseIntakeCondition',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('condition_type', models.CharField(choices=[('COSMETIC', 'Cosmetic'), ('PHYSICAL_DAMAGE', 'Physical damage'), ('LIQUID_INDICATOR', 'Liquid indicator'), ('POWER_STATE', 'Power state'), ('OTHER', 'Other')], max_length=16)),
                ('description', models.TextField(help_text='Do not record passwords, screen PINs, account credentials or OTPs.', max_length=2000)),
                ('service_case', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='intake_conditions', to='service.servicecase')),
            ],
            options={
                'ordering': ['created_at', 'id'],
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='ServiceCaseNumberSequence',
            fields=[
                ('service_center', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, primary_key=True, related_name='job_number_sequence', serialize=False, to='organization.servicecenter')),
                ('next_value', models.PositiveBigIntegerField(default=1)),
            ],
            options={
                'constraints': [models.CheckConstraint(condition=models.Q(('next_value__gte', 1)), name='service_sequence_positive')],
            },
        ),
        migrations.CreateModel(
            name='ServiceCaseWarrantySnapshot',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('recorded_coverage', models.BooleanField(default=False)),
                ('coverage_source', models.CharField(blank=True, max_length=19, null=True)),
                ('coverage_start_date', models.DateField(blank=True, null=True)),
                ('coverage_end_date', models.DateField(blank=True, null=True)),
                ('coverage_reference', models.CharField(blank=True, max_length=200, null=True)),
                ('captured_at', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
                ('service_case', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='warranty_snapshot', to='service.servicecase')),
            ],
            options={
                'ordering': ['created_at', 'id'],
                'abstract': False,
            },
        ),
        migrations.AddConstraint(
            model_name='servicecase',
            constraint=models.UniqueConstraint(fields=('service_center', 'job_number'), name='service_center_job_unique'),
        ),
        migrations.AddConstraint(
            model_name='servicecase',
            constraint=models.CheckConstraint(condition=models.Q(('job_number__regex', '^JOB-[0-9]{8,18}$')), name='service_job_format'),
        ),
        migrations.AddConstraint(
            model_name='servicecase',
            constraint=models.CheckConstraint(condition=models.Q(('intake_channel__in', ['WALK_IN', 'COURIER', 'PICKUP', 'DEALER', 'OTHER'])), name='service_channel_valid'),
        ),
        migrations.AddConstraint(
            model_name='servicecase',
            constraint=models.CheckConstraint(condition=models.Q(('status__in', ['RECEIVED', 'CANCELLED'])), name='service_status_valid'),
        ),
        migrations.AddConstraint(
            model_name='servicecase',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('cancellation_reason', ''), ('cancelled_at__isnull', True), ('status', 'RECEIVED')), models.Q(('cancelled_at__gte', models.F('received_at')), ('cancelled_at__isnull', False), ('status', 'CANCELLED')), _connector='OR'), name='service_cancellation_coherent'),
        ),
        migrations.AddConstraint(
            model_name='servicecaseaccessory',
            constraint=models.CheckConstraint(condition=models.Q(('accessory_type__in', ['CHARGER', 'CABLE', 'CASE', 'SIM', 'MEMORY_CARD', 'BOX', 'OTHER'])), name='service_accessory_type_valid'),
        ),
        migrations.AddConstraint(
            model_name='servicecaseaccessory',
            constraint=models.CheckConstraint(condition=models.Q(('quantity__gt', 0)), name='service_accessory_quantity_positive'),
        ),
        migrations.AddConstraint(
            model_name='servicecasecomplaint',
            constraint=models.UniqueConstraint(fields=('service_case', 'complaint_symptom'), name='service_case_complaint_unique'),
        ),
        migrations.AddConstraint(
            model_name='servicecaseintakecondition',
            constraint=models.CheckConstraint(condition=models.Q(('condition_type__in', ['COSMETIC', 'PHYSICAL_DAMAGE', 'LIQUID_INDICATOR', 'POWER_STATE', 'OTHER'])), name='service_condition_type_valid'),
        ),
        migrations.AddConstraint(
            model_name='servicecaseintakecondition',
            constraint=models.CheckConstraint(condition=models.Q(('description', ''), _negated=True), name='service_condition_description'),
        ),
        migrations.AddConstraint(
            model_name='servicecasewarrantysnapshot',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('coverage_end_date__isnull', True), ('coverage_reference__isnull', True), ('coverage_source__isnull', True), ('coverage_start_date__isnull', True), ('recorded_coverage', False)), models.Q(('coverage_end_date__gte', models.F('coverage_start_date')), ('coverage_end_date__isnull', False), ('coverage_reference__isnull', False), ('coverage_source__in', ['PURCHASE_EVIDENCE', 'MANUFACTURER_RECORD', 'MANUAL_OVERRIDE']), ('coverage_source__isnull', False), ('coverage_start_date__isnull', False), ('recorded_coverage', True)), _connector='OR'), name='service_snapshot_coherent'),
        ),
    ]
