<?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_display_screens')) {
            Schema::create('care_display_screens', function (Blueprint $table) {
                $table->id();
                $table->uuid('uuid')->unique();
                $table->string('owner_ref')->index();
                $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
                $table->foreignId('branch_id')->constrained('care_branches')->cascadeOnDelete();
                $table->string('name');
                $table->string('layout')->default('standard');
                $table->string('access_token')->unique();
                $table->json('service_queue_ids')->nullable();
                $table->json('settings')->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamp('last_seen_at')->nullable();
                $table->timestamps();
                $table->softDeletes();
            });
        }

        if (! Schema::hasTable('care_voice_announcements')) {
            Schema::create('care_voice_announcements', function (Blueprint $table) {
                $table->id();
                $table->string('owner_ref')->index();
                $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
                $table->foreignId('branch_id')->constrained('care_branches')->cascadeOnDelete();
                $table->foreignId('ticket_id')->nullable()->constrained('care_queue_tickets')->nullOnDelete();
                $table->string('locale', 10)->default('en');
                $table->text('message');
                $table->string('voice')->default('female');
                $table->unsignedTinyInteger('volume')->default(80);
                $table->unsignedTinyInteger('repeat_count')->default(1);
                $table->string('status')->default('pending');
                $table->timestamp('played_at')->nullable();
                $table->timestamps();

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

    public function down(): void
    {
        Schema::dropIfExists('care_voice_announcements');
        Schema::dropIfExists('care_display_screens');
    }
};
