<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Create competency_units table
        Schema::create('competency_units', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->integer('order')->default(0);
            $table->timestamps();
        });

        // Create competencies table
        Schema::create('competencies', function (Blueprint $table) {
            $table->id();
            $table->foreignId('unit_id')->constrained('competency_units')->onDelete('cascade');
            $table->foreignId('parent_id')->nullable()->constrained('competencies')->onDelete('cascade'); // For sub-competencies
            $table->string('code', 50); // e.g., "1.1", "1.1.1"
            $table->text('description');
            $table->enum('designation', ['RCIC', 'RISIA', 'BOTH'])->default('BOTH');
            $table->integer('order')->default(0);
            $table->timestamps();

            $table->index('unit_id');
            $table->index('parent_id');
            $table->index('designation');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('competencies');
        Schema::dropIfExists('competency_units');
    }
};
