<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\HR\Enums\AdjustmentDirectionEnum;
use Modules\HR\Enums\RevisionTypeEnum;
use Modules\HR\Enums\SalaryRevisionStatusEnum;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('salary_revisions', function (Blueprint $table) {
            $table->id();

            $table->foreignId('employee_id')
                ->constrained('employees')
                ->cascadeOnDelete();

            /*
             * increment  → yearly/performance increase
             * promotion  → role + salary change
             * adjustment → policy/market +/- change
             * correction → mistake fix (direct override)
             */
            $table->enum('revision_type', RevisionTypeEnum::casesAsArray());

            // ── Snapshot (apply করার আগে পুরনো values) ──────────────
            $table->decimal('old_basic_salary', 12, 2)->nullable();

            $table->foreignId('old_salary_structure_id')
                ->nullable()
                ->constrained('salary_structures')
                ->nullOnDelete();

            $table->foreignId('old_designation_id')
                ->nullable()
                ->constrained('designations')
                ->nullOnDelete();

            // ── New values ────────────────────────────────────────────
            $table->decimal('new_basic_salary', 12, 2)->nullable();

            $table->foreignId('new_designation_id')
                ->nullable()
                ->constrained('designations')
                ->nullOnDelete();

            // ── Type-specific fields ──────────────────────────────────

            // increment / adjustment: পরিবর্তনের পরিমাণ
            $table->decimal('revision_amount', 12, 2)->nullable();

            // adjustment only: বাড়বে না কমবে
            $table->enum('adjustment_direction', AdjustmentDirectionEnum::casesAsArray())->nullable();

            // correction / promotion: কারণ লেখা
            $table->text('reason')->nullable();

            // ── Promotion specific ────────────────────────────────────
            $table->boolean('clone_salary_structure')->default(false);

            $table->foreignId('cloned_structure_id')
                ->nullable()
                ->constrained('salary_structures')
                ->nullOnDelete();

            // ── Date ──────────────────────────────────────────────────
            $table->date('effective_date');

            // ── Status ────────────────────────────────────────────────
            $table->enum('salary_revision_status', SalaryRevisionStatusEnum::casesAsArray())->default(SalaryRevisionStatusEnum::PENDING);

            // ── Approval info ─────────────────────────────────────────
            $table->foreignId('approved_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            $table->text('approval_comment')->nullable();

            $table->timestamp('approved_at')->nullable();

            // ── Apply info ────────────────────────────────────────────
            $table->timestamp('applied_at')->nullable();

            // ── Audit ─────────────────────────────────────────────────
            $table->foreignId('created_by')
                ->constrained('users')
                ->cascadeOnDelete();

            $table->timestamps();

            $table->index(['employee_id', 'salary_revision_status']);
            $table->index(['effective_date']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('salary_revisions');
    }
};
