<?php

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

class CreateSuperDeductions extends Migration
{
    public function up()
    {
        Schema::create('super_deductions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('employee_id');
            $table->unsignedBigInteger('created_by'); // Admin or Director who created it
            $table->string('reason');
            $table->decimal('amount', 12, 2);
            $table->string('type');
            $table->date('deduction_date');
            $table->text('description')->nullable();
            $table->text('justification')->nullable(); 
            $table->string('status')->default('pending_approval'); // pending_approval, approved, rejected, applied
            $table->string('approval_level'); // admin or director
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->text('approval_notes')->nullable();
            $table->unsignedBigInteger('rejected_by')->nullable();
            $table->timestamp('rejected_at')->nullable();
            $table->text('rejection_reason')->nullable();
            $table->boolean('requires_director_approval')->default(false); // If amount > 100,000
            $table->timestamps();
            
            $table->foreign('employee_id')->references('id')->on('salary_employees');
            $table->foreign('created_by')->references('id')->on('users');
            $table->foreign('approved_by')->references('id')->on('users');
            $table->foreign('rejected_by')->references('id')->on('users');
            
            $table->index('status');
            $table->index('amount');
        });
    }

    public function down()
    {
        Schema::dropIfExists('super_deductions');
    }
}