<?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
    {
        if (! Schema::hasTable('care_service_queues')) {
            Schema::create('care_service_queues', function (Blueprint $table) {
                $table->id();
                $table->uuid('uuid')->unique();
                $table->string('owner_ref', 64)->index();
                $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
                $table->foreignId('branch_id')->constrained('care_branches')->cascadeOnDelete();
                $table->string('context', 64);
                $table->string('name');
                $table->string('prefix', 8);
                $table->string('routing_mode', 32)->default('shared_pool');
                $table->string('external_key')->nullable()->index();
                $table->unsignedInteger('next_number')->default(1);
                $table->boolean('is_active')->default(true);
                $table->timestamps();

                $table->unique(['organization_id', 'branch_id', 'context']);
            });
        }

        if (! Schema::hasTable('care_service_points')) {
            Schema::create('care_service_points', function (Blueprint $table) {
                $table->id();
                $table->uuid('uuid')->unique();
                $table->string('owner_ref', 64)->index();
                $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
                $table->foreignId('service_queue_id')->constrained('care_service_queues')->cascadeOnDelete();
                $table->string('name');
                $table->string('destination')->nullable();
                $table->string('kind', 32)->default('default');
                $table->unsignedBigInteger('ref_id')->nullable();
                $table->string('staff_ref')->nullable();
                $table->string('staff_display_name')->nullable();
                $table->string('external_key')->nullable()->index();
                $table->boolean('is_active')->default(true);
                $table->timestamps();

                $table->unique(['service_queue_id', 'external_key']);
            });
        }

        if (! Schema::hasTable('care_queue_tickets')) {
            Schema::create('care_queue_tickets', function (Blueprint $table) {
                $table->id();
                $table->uuid('uuid')->unique();
                $table->string('owner_ref', 64)->index();
                $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
                $table->foreignId('service_queue_id')->constrained('care_service_queues')->cascadeOnDelete();
                $table->foreignId('service_point_id')->nullable()->constrained('care_service_points')->nullOnDelete();
                $table->string('ticket_number', 32);
                $table->string('status', 32)->default('waiting')->index();
                $table->string('priority', 32)->default('walk_in');
                $table->string('source', 32)->default('api');
                $table->string('customer_name')->nullable();
                $table->string('customer_phone')->nullable();
                $table->string('care_entity', 32)->nullable();
                $table->string('care_entity_uuid', 36)->nullable()->index();
                $table->unsignedBigInteger('care_entity_id')->nullable();
                $table->unsignedBigInteger('visit_id')->nullable();
                $table->json('metadata')->nullable();
                $table->timestamp('called_at')->nullable();
                $table->timestamp('serving_at')->nullable();
                $table->timestamp('completed_at')->nullable();
                $table->timestamps();

                $table->index(['service_queue_id', 'status']);
                $table->index(['service_point_id', 'status']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('care_queue_tickets');
        Schema::dropIfExists('care_service_points');
        Schema::dropIfExists('care_service_queues');
    }
};
