<?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('payslips', function (Blueprint $table) {
            $table->id();
            $table->foreignId('staff_id')->constrained('staff')->cascadeOnDelete();
            $table->unsignedTinyInteger('period_month');   // 1-12
            $table->unsignedSmallInteger('period_year');

            // snapshot fields (printed on the payslip)
            $table->string('snap_name')->nullable();
            $table->string('snap_passport')->nullable();
            $table->string('snap_new_ic')->nullable();
            $table->string('snap_designation')->nullable();
            $table->string('snap_department')->nullable();
            $table->string('snap_branch')->nullable();
            $table->string('snap_bank_ac')->nullable();
            $table->date('snap_date_joined')->nullable();

            // earnings
            $table->decimal('basic_salary', 12, 2)->default(0);
            $table->decimal('working_days', 6, 2)->default(0);
            $table->unsignedSmallInteger('days_divisor')->default(26);
            $table->decimal('working_units', 8, 2)->default(0);
            $table->decimal('al_amount', 12, 2)->default(0);
            $table->decimal('ul_amount', 12, 2)->default(0);
            $table->decimal('ot_hours', 8, 2)->default(0);
            $table->decimal('ot_days', 6, 2)->default(0);
            $table->decimal('ot_amount', 12, 2)->default(0);
            $table->decimal('ph_hours', 8, 2)->default(0);
            $table->decimal('ph_days', 6, 2)->default(0);
            $table->decimal('ph_amount', 12, 2)->default(0);
            $table->decimal('total_earning', 12, 2)->default(0);

            // fixed deductions
            $table->decimal('epf_employee', 12, 2)->default(0);
            $table->decimal('socso_employee', 12, 2)->default(0);
            $table->decimal('eis_employee', 12, 2)->default(0);
            $table->decimal('income_tax_pcb', 12, 2)->default(0);
            $table->decimal('income_tax_cp38', 12, 2)->default(0);
            $table->decimal('unpaid_leave', 12, 2)->default(0);
            $table->decimal('advance', 12, 2)->default(0);
            $table->decimal('loan', 12, 2)->default(0);

            $table->decimal('total_deductions', 12, 2)->default(0);
            $table->decimal('allowance_total', 12, 2)->default(0);
            $table->decimal('nett_pay', 12, 2)->default(0);

            $table->string('status')->default('draft');   // draft|finalized
            $table->string('pdf_path')->nullable();
            $table->foreignId('generated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['staff_id', 'period_year', 'period_month']);
            $table->index(['period_year', 'period_month']);
        });
    }

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