# Generated by Django 5.2.17 on 2026-09-30 16:55

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', '0006_handover_closure'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='AppointmentSlot',
            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)),
                ('date', models.DateField()),
                ('start_time', models.TimeField()),
                ('end_time', models.TimeField()),
                ('capacity', models.PositiveIntegerField()),
                ('is_active', models.BooleanField(default=True)),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('service_center', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.servicecenter')),
            ],
            options={
                'ordering': ['date', 'start_time', 'pk'],
                'permissions': [('manage_slots', 'Configure appointment availability')],
                'default_permissions': (),
            },
        ),
        migrations.CreateModel(
            name='Appointment',
            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)),
                ('reason', models.CharField(blank=True, max_length=2000)),
                ('channel', models.CharField(choices=[('PHONE', 'Phone'), ('DESK', 'Front desk'), ('OTHER', 'Other')], max_length=10)),
                ('status', models.CharField(choices=[('SCHEDULED', 'Scheduled'), ('CHECKED_IN', 'Checked in'), ('COMPLETED', 'Completed'), ('CANCELLED', 'Cancelled'), ('NO_SHOW', 'No show')], default='SCHEDULED', max_length=10)),
                ('checked_in_at', models.DateTimeField(blank=True, null=True)),
                ('ended_at', models.DateTimeField(blank=True, null=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.company')),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('customer', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='customers.customer')),
                ('device', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='devices.device')),
                ('service_center', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.servicecenter')),
                ('slot', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='appointments', to='frontdesk.appointmentslot')),
            ],
            options={
                'ordering': ['slot__date', 'slot__start_time', 'created_at', 'pk'],
                'permissions': [('view_appointment', 'View appointments'), ('create_appointment', 'Book appointments'), ('cancel_appointment', 'Cancel appointments or record no show'), ('check_in_appointment', 'Check in appointments')],
                'default_permissions': (),
            },
        ),
        migrations.CreateModel(
            name='QueueEntry',
            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)),
                ('business_date', models.DateField()),
                ('token', models.PositiveBigIntegerField()),
                ('arrived_at', models.DateTimeField(default=django.utils.timezone.now)),
                ('called_at', models.DateTimeField(blank=True, null=True)),
                ('serving_at', models.DateTimeField(blank=True, null=True)),
                ('ended_at', models.DateTimeField(blank=True, null=True)),
                ('status', models.CharField(choices=[('WAITING', 'Waiting'), ('CALLED', 'Called'), ('SERVING', 'Serving'), ('COMPLETED', 'Completed'), ('CANCELLED', 'Cancelled')], default='WAITING', max_length=10)),
                ('reason', models.CharField(blank=True, max_length=2000)),
                ('appointment', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='queue_entry', to='frontdesk.appointment')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.company')),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('customer', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='customers.customer')),
                ('device', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='devices.device')),
                ('service_case', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='frontdesk_visit', to='service.servicecase')),
                ('service_center', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.servicecenter')),
            ],
            options={
                'ordering': ['business_date', 'token'],
                'permissions': [('view_queue', 'View front-desk queue'), ('manage_queue', 'Manage front-desk arrivals and queue')],
                'default_permissions': (),
            },
        ),
        migrations.CreateModel(
            name='FrontdeskEvent',
            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)),
                ('action', models.CharField(max_length=40)),
                ('reason', models.CharField(blank=True, max_length=2000)),
                ('occurred_at', models.DateTimeField(default=django.utils.timezone.now)),
                ('actor', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('appointment', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='events', to='frontdesk.appointment')),
                ('slot', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='events', to='frontdesk.appointmentslot')),
                ('queue_entry', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='events', to='frontdesk.queueentry')),
            ],
            options={
                'ordering': ['occurred_at', 'id'],
                'default_permissions': (),
            },
        ),
        migrations.CreateModel(
            name='QueueSequence',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('business_date', models.DateField()),
                ('next_value', models.PositiveBigIntegerField(default=1)),
                ('service_center', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.servicecenter')),
            ],
            options={
                'default_permissions': (),
            },
        ),
        migrations.AddConstraint(
            model_name='appointmentslot',
            constraint=models.CheckConstraint(condition=models.Q(('capacity__gte', 1)), name='frontdesk_capacity_positive'),
        ),
        migrations.AddConstraint(
            model_name='appointmentslot',
            constraint=models.CheckConstraint(condition=models.Q(('end_time__gt', models.F('start_time'))), name='frontdesk_slot_time_order'),
        ),
        migrations.AddConstraint(
            model_name='appointmentslot',
            constraint=models.UniqueConstraint(fields=('service_center', 'date', 'start_time'), name='frontdesk_slot_unique'),
        ),
        migrations.AddConstraint(
            model_name='appointment',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('checked_in_at', None), ('ended_at', None), ('status', 'SCHEDULED')), models.Q(('checked_in_at__isnull', False), ('ended_at', None), ('status', 'CHECKED_IN')), models.Q(('checked_in_at__isnull', False), ('ended_at__isnull', False), ('status', 'COMPLETED')), models.Q(('ended_at__isnull', False), ('status', 'CANCELLED')), models.Q(('checked_in_at', None), ('ended_at__isnull', False), ('status', 'NO_SHOW')), _connector='OR'), name='frontdesk_appointment_state'),
        ),
        migrations.AddConstraint(
            model_name='queueentry',
            constraint=models.UniqueConstraint(fields=('service_center', 'business_date', 'token'), name='frontdesk_token_unique'),
        ),
        migrations.AddConstraint(
            model_name='queueentry',
            constraint=models.CheckConstraint(condition=models.Q(('token__gte', 1)), name='frontdesk_token_positive'),
        ),
        migrations.AddConstraint(
            model_name='queueentry',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('called_at', None), ('ended_at', None), ('serving_at', None), ('status', 'WAITING')), models.Q(('called_at__isnull', False), ('ended_at', None), ('serving_at', None), ('status', 'CALLED')), models.Q(('called_at__isnull', False), ('ended_at', None), ('serving_at__isnull', False), ('status', 'SERVING')), models.Q(('called_at__isnull', False), ('ended_at__isnull', False), ('serving_at__isnull', False), ('status', 'COMPLETED')), models.Q(('ended_at__isnull', False), ('service_case', None), ('status', 'CANCELLED')), _connector='OR'), name='frontdesk_queue_state'),
        ),
        migrations.AddConstraint(
            model_name='queuesequence',
            constraint=models.UniqueConstraint(fields=('service_center', 'business_date'), name='frontdesk_sequence_unique'),
        ),
        migrations.AddConstraint(
            model_name='queuesequence',
            constraint=models.CheckConstraint(condition=models.Q(('next_value__gte', 1)), name='frontdesk_sequence_positive'),
        ),
    ]
