<?php

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

class AddColumnsToPengajuanKeuangansTable extends Migration
{
    public function up()
    {
        Schema::table('pengajuan_keuangans', function (Blueprint $table) {
            $table->text('deskripsi')->nullable()->after('nominal');
            $table->string('file_pendukung')->nullable()->after('deskripsi');
            $table->enum('kategori', [
                'Operasional',
                'Sertifikasi', 
                'Pelatihan',
                'Sarana Prasarana',
                'Lainnya'
            ])->default('Operasional')->after('judul');
            
            // Siapa yang approve di tiap level
            $table->unsignedBigInteger('approved_by_admin')->nullable()->after('alasan_tolak');
            $table->unsignedBigInteger('approved_by_manajer')->nullable()->after('approved_by_admin');
            $table->unsignedBigInteger('approved_by_direktur')->nullable()->after('approved_by_manajer');
            
            // Timestamp tiap level
            $table->timestamp('tgl_approve_admin')->nullable()->after('approved_by_direktur');
            $table->timestamp('tgl_approve_manajer')->nullable()->after('tgl_approve_admin');
            $table->timestamp('tgl_approve_direktur')->nullable()->after('tgl_approve_manajer');

            $table->foreign('approved_by_admin')->references('id')->on('users')->nullOnDelete();
            $table->foreign('approved_by_manajer')->references('id')->on('users')->nullOnDelete();
            $table->foreign('approved_by_direktur')->references('id')->on('users')->nullOnDelete();
        });
    }

    public function down()
    {
        Schema::table('pengajuan_keuangans', function (Blueprint $table) {
            $table->dropForeign(['approved_by_admin']);
            $table->dropForeign(['approved_by_manajer']);
            $table->dropForeign(['approved_by_direktur']);
            $table->dropColumn([
                'deskripsi',
                'file_pendukung',
                'kategori',
                'approved_by_admin',
                'approved_by_manajer',
                'approved_by_direktur',
                'tgl_approve_admin',
                'tgl_approve_manajer',
                'tgl_approve_direktur',
            ]);
        });
    }
}