# Generated by Django 5.2.17 on 2026-09-27 08:47

import django.core.validators
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):

    dependencies = [
        ('customers', '0002_customeraddress_customercontact'),
        ('devices', '0003_customer_device_relationship'),
        ('organization', '0002_userorganizationassignment'),
        ('service', '0003_diagnostic_assessment'),
        ('service_catalog', '0002_technical_taxonomy'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ServiceRepairAction',
            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)),
                ('note', models.TextField(blank=True, help_text='Do not record passwords, screen PINs, account credentials or OTPs.', max_length=2000, validators=[django.core.validators.MaxLengthValidator(2000)])),
                ('is_active', models.BooleanField(default=True)),
                ('performed_at', models.DateTimeField(blank=True, null=True)),
            ],
            options={
                'ordering': ['created_at', 'id'],
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='ServiceRepairExecution',
            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)),
                ('status', models.CharField(choices=[('OPEN', 'Open'), ('COMPLETED', 'Completed'), ('ABANDONED', 'Abandoned')], default='OPEN', max_length=9)),
                ('note', models.TextField(blank=True, help_text='Do not record passwords, screen PINs, account credentials or OTPs.', max_length=4000, validators=[django.core.validators.MaxLengthValidator(4000)])),
                ('started_at', models.DateTimeField(default=django.utils.timezone.now)),
                ('completed_at', models.DateTimeField(blank=True, null=True)),
                ('outcome', models.CharField(blank=True, choices=[('REPAIRED', 'Repaired'), ('NOT_REPAIRED', 'Not repaired')], max_length=12, null=True)),
                ('abandoned_at', models.DateTimeField(blank=True, null=True)),
                ('abandon_reason', models.CharField(blank=True, help_text='Do not record passwords, screen PINs, account credentials or OTPs.', max_length=2000)),
            ],
            options={
                'ordering': ['started_at', 'id'],
                'abstract': False,
            },
        ),
        migrations.RemoveConstraint(
            model_name='servicecase',
            name='service_status_valid',
        ),
        migrations.RemoveConstraint(
            model_name='servicecase',
            name='service_cancellation_coherent',
        ),
        migrations.AlterField(
            model_name='servicecase',
            name='status',
            field=models.CharField(choices=[('RECEIVED', 'Received'), ('ASSIGNED', 'Assigned'), ('DIAGNOSING', 'Diagnosing'), ('DIAGNOSED', 'Diagnosed'), ('REPAIRING', 'Repairing'), ('REPAIRED', 'Repaired'), ('CANCELLED', 'Cancelled')], default='RECEIVED', max_length=10),
        ),
        migrations.AddConstraint(
            model_name='servicecase',
            constraint=models.CheckConstraint(condition=models.Q(('status__in', ['RECEIVED', 'ASSIGNED', 'DIAGNOSING', 'DIAGNOSED', 'REPAIRING', 'REPAIRED', '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__in', ['RECEIVED', 'ASSIGNED', 'DIAGNOSING', 'DIAGNOSED', 'REPAIRING', 'REPAIRED'])), models.Q(('cancelled_at__gte', models.F('received_at')), ('cancelled_at__isnull', False), ('status', 'CANCELLED')), _connector='OR'), name='service_cancellation_coherent'),
        ),
        migrations.AddField(
            model_name='servicerepairaction',
            name='performed_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='performed_repair_actions', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='servicerepairaction',
            name='repair_action',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='service_repair_actions', to='service_catalog.repairaction'),
        ),
        migrations.AddField(
            model_name='servicerepairexecution',
            name='abandoned_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='abandoned_repairs', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='servicerepairexecution',
            name='completed_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='completed_repairs', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='servicerepairexecution',
            name='diagnostic_assessment',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='repair_executions', to='service.servicediagnosticassessment'),
        ),
        migrations.AddField(
            model_name='servicerepairexecution',
            name='engineer_assignment',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='repair_executions', to='service.serviceengineerassignment'),
        ),
        migrations.AddField(
            model_name='servicerepairexecution',
            name='service_case',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='repair_executions', to='service.servicecase'),
        ),
        migrations.AddField(
            model_name='servicerepairexecution',
            name='started_by',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='started_repairs', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='servicerepairaction',
            name='repair_execution',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='actions', to='service.servicerepairexecution'),
        ),
        migrations.AddConstraint(
            model_name='servicerepairexecution',
            constraint=models.UniqueConstraint(condition=models.Q(('status', 'OPEN')), fields=('service_case',), name='service_one_open_repair'),
        ),
        migrations.AddConstraint(
            model_name='servicerepairexecution',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('abandon_reason', ''), ('abandoned_at__isnull', True), ('abandoned_by__isnull', True), ('completed_at__isnull', True), ('completed_by__isnull', True), ('outcome__isnull', True), ('status', 'OPEN')), models.Q(('abandon_reason', ''), ('abandoned_at__isnull', True), ('abandoned_by__isnull', True), ('completed_at__gte', models.F('started_at')), ('completed_at__isnull', False), ('completed_by__isnull', False), ('outcome__in', ['REPAIRED', 'NOT_REPAIRED']), ('outcome__isnull', False), ('status', 'COMPLETED')), models.Q(('abandoned_at__gte', models.F('started_at')), ('abandoned_at__isnull', False), ('abandoned_by__isnull', False), ('completed_at__isnull', True), ('completed_by__isnull', True), ('outcome__isnull', True), ('status', 'ABANDONED'), models.Q(('abandon_reason', ''), _negated=True)), _connector='OR'), name='service_repair_outcome_coherent'),
        ),
        migrations.AddConstraint(
            model_name='servicerepairaction',
            constraint=models.UniqueConstraint(condition=models.Q(('is_active', True)), fields=('repair_execution', 'repair_action'), name='service_active_repair_action_unique'),
        ),
        migrations.AddConstraint(
            model_name='servicerepairaction',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('performed_at__isnull', True), ('performed_by__isnull', True)), models.Q(('performed_at__gte', models.F('created_at')), ('performed_at__isnull', False), ('performed_by__isnull', False)), _connector='OR'), name='service_repair_performed_coherent'),
        ),
    ]
