<?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('payroll_component_definitions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable();
            $table->string('code', 64); // BASIC, HRA, SPECIAL, PF_EMP, PF_EMPR, ESI_EMP, ESI_EMPR, PT, LOP
            $table->string('name');
            $table->string('category', 32); // earning, deduction, employer_contribution
            $table->string('value_type', 32)->default('fixed'); // fixed, percent_of_ctc, percent_of_basic, formula
            $table->decimal('value', 15, 4)->nullable();
            $table->boolean('is_taxable')->default(true);
            $table->unsignedInteger('display_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['company_id', 'code']);
            $table->foreign('company_id')->references('id')->on('companies')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('payroll_component_definitions');
    }
};
