<?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('payslip_lines', function (Blueprint $table) {
            $table->id();
            $table->foreignId('payslip_id')->constrained('payslips')->cascadeOnDelete();
            $table->foreignId('pay_component_id')->nullable()->constrained('pay_components')->nullOnDelete();

            // Snapshot of the component at calculation time.
            $table->string('code');
            $table->string('name');
            $table->string('type', 16);          // EARNING | DEDUCTION | STATUTORY

            $table->decimal('base', 15, 2)->nullable();  // base the amount was computed from
            $table->decimal('rate', 9, 4)->nullable();
            $table->decimal('amount', 15, 2);            // signed positive; type dictates +/-

            $table->integer('sort')->default(0);
            $table->timestamps();

            $table->index(['payslip_id', 'type']);
        });
    }

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