<?php

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

class AddColumnsToCutiTable extends Migration
{
    public function up()
    {
        Schema::table('cuti', function (Blueprint $table) {
            if (!Schema::hasColumn('cuti', 'user_id')) {
                $table->foreignId('user_id')->after('id')->constrained('users')->onDelete('cascade');
            }
            if (!Schema::hasColumn('cuti', 'nama')) {
                $table->string('nama')->after('user_id');
            }
            if (!Schema::hasColumn('cuti', 'jenis_cuti')) {
                $table->string('jenis_cuti');
            }
            if (!Schema::hasColumn('cuti', 'tanggal_mulai')) {
                $table->date('tanggal_mulai');
            }
            if (!Schema::hasColumn('cuti', 'tanggal_selesai')) {
                $table->date('tanggal_selesai');
            }
            if (!Schema::hasColumn('cuti', 'keterangan')) {
                $table->text('keterangan');
            }
            if (!Schema::hasColumn('cuti', 'status')) {
                $table->enum('status', ['Pending', 'Disetujui', 'Ditolak'])->default('Pending');
            }
            if (!Schema::hasColumn('cuti', 'file_pdf')) {
                $table->string('file_pdf')->nullable();
            }
            if (!Schema::hasColumn('cuti', 'approved_by')) {
                $table->foreignId('approved_by')->nullable()->constrained('users');
            }
            if (!Schema::hasColumn('cuti', 'approved_at')) {
                $table->timestamp('approved_at')->nullable();
            }
            if (!Schema::hasColumn('cuti', 'rejected_by')) {
                $table->foreignId('rejected_by')->nullable()->constrained('users');
            }
            if (!Schema::hasColumn('cuti', 'rejected_at')) {
                $table->timestamp('rejected_at')->nullable();
            }
        });
    }

    public function down()
    {
        Schema::table('cuti', function (Blueprint $table) {
            $table->dropForeign(['user_id']);
            $table->dropForeign(['approved_by']);
            $table->dropForeign(['rejected_by']);
            $table->dropColumn([
                'user_id',
                'nama',
                'jenis_cuti',
                'tanggal_mulai',
                'tanggal_selesai',
                'keterangan',
                'status',
                'file_pdf',
                'approved_by',
                'approved_at',
                'rejected_by',
                'rejected_at'
            ]);
        });
    }
} 