<?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
      {

            /*
        |--------------------------------------------------------------------------
        | TABEL PENUGASAN
        |--------------------------------------------------------------------------
        */

            Schema::create('penugasan', function (Blueprint $table) {

                  $table->id();

                  $table->string('nomor_surat')
                        ->unique()
                        ->nullable();

                  $table->string('kegiatan');

                  $table->string('lokasi')->nullable();

                  $table->text('keterangan')->nullable();

                  $table->string('lampiran')->nullable();

                  $table->date('tanggal_mulai');

                  $table->date('tanggal_selesai')
                        ->nullable();

                  $table->enum('status', [
                        'proses',
                        'selesai',
                        'telah_ditinjau',
                        'dibatalkan'
                  ])->default('proses');

                  /*
            |--------------------------------------------------------------------------
            | FOREIGN KEY
            |--------------------------------------------------------------------------
            */

                  $table->foreignId('created_by')
                        ->constrained('users')
                        ->onDelete('cascade');

                  $table->foreignId('approved_by')
                        ->nullable()
                        ->constrained('users')
                        ->onDelete('set null');

                  $table->timestamp('approved_at')
                        ->nullable();

                  /*
            |--------------------------------------------------------------------------
            | LAINNYA
            |--------------------------------------------------------------------------
            */

                  $table->text('alasan_hapus')
                        ->nullable();

                  $table->softDeletes();

                  $table->timestamps();
            });

            /*
        |--------------------------------------------------------------------------
        | TABEL PIVOT PENUGASAN PEGAWAI
        |--------------------------------------------------------------------------
        */

            Schema::create('penugasan_pegawai', function (Blueprint $table) {

                  $table->id();

                  $table->foreignId('penugasan_id')
                        ->constrained('penugasan')
                        ->onDelete('cascade');

                  // FIX DI SINI: Mereferensikan ke tabel users karena pegawai mengambil dari role user
                  $table->foreignId('pegawai_id')
                        ->constrained('users')
                        ->onDelete('cascade');

                  $table->timestamps();
            });
      }

      public function down(): void
      {

            Schema::dropIfExists('penugasan_pegawai');

            Schema::dropIfExists('penugasan');
      }
};
