<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('queue_entries', function (Blueprint $table) {
            $table->id();
            $table->foreignId('organization_id')->constrained()->cascadeOnDelete();
            $table->foreignId('appointment_id')->constrained()->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained()->restrictOnDelete();
            $table->foreignId('doctor_id')->constrained()->restrictOnDelete();
            $table->foreignId('location_id')->constrained()->restrictOnDelete();
            $table->string('queue_scope_key', 120);
            $table->date('queue_date');
            $table->unsignedInteger('token_number');
            $table->enum('status', ['checked_in', 'waiting', 'with_doctor', 'completed', 'no_show', 'cancelled'])->default('waiting')->index();
            $table->dateTime('arrival_at')->nullable();
            $table->dateTime('sent_to_doctor_at')->nullable();
            $table->dateTime('completed_at')->nullable();
            $table->dateTime('no_show_at')->nullable();
            $table->foreignId('created_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['organization_id', 'queue_scope_key', 'queue_date', 'token_number'], 'queue_token_unique');
            $table->unique(['appointment_id', 'queue_date'], 'appointment_queue_date_unique');
            $table->index(['organization_id', 'queue_scope_key', 'queue_date', 'status'], 'queue_scope_status_idx');
            $table->index(['doctor_id', 'location_id', 'queue_date'], 'queue_doctor_location_date_idx');
        });

        Schema::create('queue_status_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('queue_entry_id')->constrained()->cascadeOnDelete();
            $table->string('event_type', 60)->default('status_changed');
            $table->enum('from_status', ['checked_in', 'waiting', 'with_doctor', 'completed', 'no_show', 'cancelled'])->nullable();
            $table->enum('to_status', ['checked_in', 'waiting', 'with_doctor', 'completed', 'no_show', 'cancelled'])->nullable();
            $table->unsignedInteger('old_token_number')->nullable();
            $table->unsignedInteger('new_token_number')->nullable();
            $table->string('reason', 500)->nullable();
            $table->foreignId('changed_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['queue_entry_id', 'created_at']);
        });

        Schema::create('queue_counters', function (Blueprint $table) {
            $table->id();
            $table->foreignId('organization_id')->constrained()->cascadeOnDelete();
            $table->string('queue_scope_key', 120);
            $table->date('queue_date');
            $table->unsignedInteger('last_allocated_token')->default(0);
            $table->unsignedInteger('current_served_token')->nullable();
            $table->timestamps();

            $table->unique(['organization_id', 'queue_scope_key', 'queue_date'], 'queue_counter_scope_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('queue_counters');
        Schema::dropIfExists('queue_status_histories');
        Schema::dropIfExists('queue_entries');
    }
};
