<?php

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

return new class extends Migration
{
    public function up()
    {
        Schema::create('claims', function (Blueprint $table) {
            $table->id();
            $table->date('claim_date');

            // User FK (int unsigned)
            $table->unsignedInteger('user_id')->nullable();
            $table->foreign('user_id')->references('id')->on('users')->nullOnDelete();

            // Vehicle type FK
            $table->unsignedBigInteger('jenis_kenderaan_id');
            $table->foreign('jenis_kenderaan_id')->references('id')->on('lkp_jenis_kenderaans')->onDelete('cascade');

            // Project FK
            $table->unsignedInteger('project_id')->nullable();
            $table->foreign('project_id')->references('id')->on('lkp_project');

            // distance_from is always "Pejabat"
            $table->string('distance_from')->default('Pejabat');

            // User input fields
            $table->string('distance_to');
            $table->string('description')->nullable();
            $table->decimal('total_mileage', 8, 2);
            $table->decimal('toll_amount', 8, 2)->nullable();
            $table->decimal('others_amount', 8, 2)->nullable();
            $table->string('others_description')->nullable();

            $table->decimal('calculated_amount', 10, 2)->nullable(); // system auto-calculated RM
            $table->decimal('penalty_amount', 10, 2)->default(0);   // system-applied penalty if late
            $table->string('penalty_reason')->nullable();
            $table->decimal('total_claim_amount', 10, 2)->default(0);

            // Add column for verified_by (optional, allows null)
            $table->unsignedInteger('verified_by')->nullable(); // Added verified_by column
            $table->foreign('verified_by')->references('id')->on('users')->nullOnDelete(); // Foreign key for verified_by
            
            // Add column for approved_by (optional, allows null)
            $table->unsignedInteger('approved_by')->nullable();
            $table->foreign('approved_by')->references('id')->on('users')->nullOnDelete();
            
            
            // Add column for reviewed_by (optional, allows null) for finance
            $table->unsignedInteger('reviewed_by')->nullable(); // Added reviewed_by column
            $table->foreign('reviewed_by')->references('id')->on('users')->nullOnDelete(); // Foreign key for reviewed_by
            
            // Rejected by (int unsigned)
            $table->unsignedInteger('rejected_by')->nullable();
            $table->foreign('rejected_by')->references('id')->on('users')->nullOnDelete();

            // Reject reason
            $table->string('reject_reason', 500)->nullable();


            // Claim status (workflow)
            $table->enum('status', [
                'Diproses',
                'Disokong',
                'Disahkan',
                'Disemak',
                'Diluluskan',
                'Ditolak',
                'Dibetulkan',
                'Dibayar'
            ])->default('Diproses');
            
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::table('claims', function (Blueprint $table) {
            // Drop the foreign key constraints and the column
            $table->dropForeign(['approved_by']);
            $table->dropColumn('approved_by');
            
            $table->dropForeign(['verified_by']);
            $table->dropColumn('verified_by');
            
            $table->dropForeign(['reviewed_by']);
            $table->dropColumn('reviewed_by');
            
            $table->dropForeign(['rejected_by']);
            $table->dropColumn('rejected_by');

            $table->dropColumn('reject_reason');
        });

        // Drop the 'claims' table entirely
        Schema::dropIfExists('claims');
    }
};
