<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('service_requests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('academy_id')->constrained('academies');
            $table->uuid('uuid')->unique();
            $table->foreignId('user_id')->constrained('users');
            $table->foreignId('participant_id')->constrained('participants');
            $table->string('type', 30);
            $table->string('status', 30)->default('pending');
            $table->text('reason');
            $table->jsonb('metadata')->default('{}');
            $table->text('admin_notes')->nullable();
            $table->foreignId('handled_by')->nullable()->constrained('users');
            $table->timestamp('handled_at')->nullable();
            $table->timestamps();

            $table->index(['academy_id', 'status']);
            $table->index(['participant_id', 'type']);
            $table->index(['user_id', 'created_at']);
        });

        DB::statement("ALTER TABLE service_requests ADD CONSTRAINT service_requests_type_check CHECK (type IN ('freeze', 'unfreeze', 'transfer', 'cancellation', 'other'))");
        DB::statement("ALTER TABLE service_requests ADD CONSTRAINT service_requests_status_check CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled'))");
    }

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