<?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('employee_payrolls', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable();
            $table->unsignedBigInteger('payroll_cycle_id');
            $table->unsignedBigInteger('user_id');
            $table->decimal('ctc_annual', 15, 2)->nullable();
            $table->decimal('gross_monthly', 15, 2)->default(0);
            $table->decimal('net_monthly', 15, 2)->default(0);
            $table->decimal('total_deductions', 15, 2)->default(0);
            $table->decimal('lop_days', 8, 2)->default(0);
            $table->decimal('lop_amount', 15, 2)->default(0);
            $table->unsignedInteger('working_days')->default(0);
            $table->decimal('paid_days', 8, 2)->default(0);
            $table->string('status', 20)->default('draft'); // draft, calculated, finalized
            $table->date('payment_date')->nullable();
            $table->timestamps();

            $table->unique(['payroll_cycle_id', 'user_id']);
            $table->foreign('company_id')->references('id')->on('companies')->nullOnDelete();
            $table->foreign('payroll_cycle_id')->references('id')->on('payroll_cycles')->cascadeOnDelete();
            $table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('employee_payrolls');
    }
};
