<?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
    {
        Schema::create('kompetensi', function (Blueprint $table) {
            $table->id();
            $table->string('nama_kompetensi');
            $table->enum('jenis_kompetensi', ['manajerial', 'sosial_kultural']);
            $table->text('deskripsi')->nullable();
            $table->timestamps();
        });

        Schema::create('indikator_kompetensi', function (Blueprint $table) {
            $table->id();
            $table->foreignId('kompetensi_id')->constrained('kompetensi')->cascadeOnDelete();
            $table->foreignId('jenjang_jabatan_id')
                ->nullable()
                ->constrained('jenjang_jabatan')
                ->nullOnDelete();
            $table->string('kode_indikator', 50);
            $table->text('pertanyaan');
            $table->timestamps();
        });

        Schema::create('standar_kompetensi_jabatan', function (Blueprint $table) {
            $table->id();
            $table->foreignId('jabatan_fungsional_id')->constrained('jabatan_fungsional')->cascadeOnDelete();
            $table->foreignId('kompetensi_id')->constrained('kompetensi')->cascadeOnDelete();
            $table->unsignedTinyInteger('level')->default(1);
            $table->text('deskripsi')->nullable();
            $table->timestamps();

            $table->unique(['jabatan_fungsional_id', 'kompetensi_id'], 'standar_jabatan_kompetensi_unique');
        });

        Schema::create('assessment_kompetensi', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pegawai_id')->constrained('pegawai')->cascadeOnDelete();
            $table->foreignId('indikator_kompetensi_id')->constrained('indikator_kompetensi')->cascadeOnDelete();
            $table->enum('jawaban', ['ya', 'tidak']);
            $table->unsignedSmallInteger('periode_tahun');
            $table->enum('status', ['draft', 'submitted', 'verified'])->default('draft');
            $table->timestamps();

            $table->unique(['pegawai_id', 'indikator_kompetensi_id', 'periode_tahun'], 'assessment_pegawai_indikator_periode_unique');
        });

        Schema::create('rekomendasi_pelatihan', function (Blueprint $table) {
            $table->id();
            $table->foreignId('kompetensi_id')->constrained('kompetensi')->cascadeOnDelete();
            $table->foreignId('jenjang_jabatan_id')->constrained('jenjang_jabatan')->cascadeOnDelete();
            $table->string('nama_pelatihan');
            $table->string('jenis_pelatihan')->nullable();
            $table->enum('bentuk_pelatihan', ['klasikal', 'non_klasikal']);
            $table->text('deskripsi')->nullable();
            $table->enum('status', ['aktif', 'nonaktif'])->default('aktif');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('rekomendasi_pelatihan');
        Schema::dropIfExists('assessment_kompetensi');
        Schema::dropIfExists('standar_kompetensi_jabatan');
        Schema::dropIfExists('indikator_kompetensi');
        Schema::dropIfExists('kompetensi');
    }
};
