<?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('leave_types', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code')->unique();
            $table->integer('days_allowed_per_year')->default(0);
            $table->boolean('requires_approval')->default(true);
            $table->boolean('is_paid')->default(true);
            $table->string('color')->default('#007bff');
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('leave_balances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->foreignId('leave_type_id')->constrained()->cascadeOnDelete();
            $table->year('year');
            $table->decimal('entitled_days', 5, 1)->default(0);
            $table->decimal('taken_days', 5, 1)->default(0);
            $table->decimal('pending_days', 5, 1)->default(0);
            $table->timestamps();
            $table->unique(['employee_id', 'leave_type_id', 'year']);
        });

        Schema::create('leaves', function (Blueprint $table) {
            $table->id();
            $table->string('leave_number')->unique();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->foreignId('leave_type_id')->constrained()->cascadeOnDelete();
            $table->date('date_of_request');
            $table->date('date_from');
            $table->date('date_to');
            $table->integer('duration_weeks')->nullable();
            $table->integer('duration_months')->nullable();
            $table->decimal('total_days', 5, 1)->nullable();
            $table->string('forwarding_address')->nullable();
            $table->text('duty_arrangement')->nullable();
            $table->text('reason')->nullable();
            $table->enum('status', ['pending', 'dept_approved', 'hr_approved', 'approved', 'rejected', 'cancelled'])->default('pending');
            // Approval chain
            $table->foreignId('dept_head_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('dept_approved_at')->nullable();
            $table->text('dept_comments')->nullable();
            $table->foreignId('hr_approver_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('hr_approved_at')->nullable();
            $table->text('hr_comments')->nullable();
            $table->foreignId('president_approver_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('president_approved_at')->nullable();
            $table->text('president_comments')->nullable();
            $table->text('rejection_reason')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('leaves');
        Schema::dropIfExists('leave_balances');
        Schema::dropIfExists('leave_types');
    }
};
