<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\Core\Enums\StatusEnum;
use Modules\HR\Enums\CalculationBasisEnum;
use Modules\HR\Enums\SalaryStructureCalculationTypeEnum;

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

            $table->foreignId('salary_structure_id')
                ->constrained()
                ->cascadeOnDelete();

            $table->foreignId('salary_component_id')
                ->constrained('salary_components')
                ->cascadeOnDelete();

            /*
            * true  => use company default rule (salary_components)
            * false => override per employee
            */

            $table->boolean('use_default_rule')
                ->default(true);

            /*
            * SAME STRUCTURE AS salary_components
            * (only used when use_default_rule = false)
            */

            $table->enum('calculation_type', SalaryStructureCalculationTypeEnum::casesAsArray())->nullable();

            // Used for both fixed value and percentage value
            $table->decimal('calculation_value', 12, 2)
                ->nullable();
            /*

            /*
            // basic / gross / custom rule reference
            //for formula base calculation (basic*0.5% or gross*5%)
            */

            $table->string('formula')
                ->nullable();

            $table->enum('calculation_basis', [
                CalculationBasisEnum::casesAsArray()
            ])->nullable();

            $table->boolean('is_basic')
                ->default(false);

            $table->enum('status', StatusEnum::casesAsArray())->default(StatusEnum::ACTIVE);

            $table->timestamps();
        });
    }

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