<?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('evaluation_criteria', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->string('name_ar');
            $table->string('name')->nullable();
            $table->text('description_ar')->nullable();
            $table->string('category', 50)->nullable();
            $table->integer('max_score')->default(10);
            $table->integer('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->jsonb('metadata')->default('{}');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['academy_id', 'is_active']);
            $table->index('category');
        });

        Schema::create('evaluations', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('participant_id')->constrained('participants');
            $table->foreignId('training_group_id')->nullable()->constrained('training_groups');
            $table->foreignId('evaluator_id')->constrained('users');
            $table->date('evaluation_date');
            $table->date('period_from')->nullable();
            $table->date('period_to')->nullable();
            $table->decimal('overall_score', 5, 2)->nullable();
            $table->text('overall_notes')->nullable();
            $table->string('status', 20)->default('draft');
            $table->foreignId('approved_by')->nullable()->constrained('users');
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('shared_at')->nullable();
            $table->jsonb('metadata')->default('{}');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['academy_id', 'participant_id']);
            $table->index('evaluator_id');
            $table->index('training_group_id');
            $table->index('status');
        });

        DB::statement("ALTER TABLE evaluations ADD CONSTRAINT evaluations_status_check CHECK (status IN ('draft', 'submitted', 'approved', 'shared'))");

        Schema::create('evaluation_scores', function (Blueprint $table) {
            $table->id();
            $table->foreignId('evaluation_id')->constrained('evaluations')->cascadeOnDelete();
            $table->foreignId('criterion_id')->constrained('evaluation_criteria');
            $table->decimal('score', 5, 2);
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index('evaluation_id');
            $table->index('criterion_id');
            $table->unique(['evaluation_id', 'criterion_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('evaluation_scores');
        Schema::dropIfExists('evaluations');
        Schema::dropIfExists('evaluation_criteria');
    }
};
