<?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('payrolls', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('payroll_period_id')->unsigned();
            $table->bigInteger('employee_id')->unsigned();
            $table->bigInteger('branch_id')->unsigned()->nullable()->comment('Cost center spesifik karyawan saat digaji');
            $table->string('payslip_number', 100)->unique();
            $table->decimal('total_earnings', 15, 2)->default(0.00);
            $table->decimal('total_deductions', 15, 2)->default(0.00);
            $table->decimal('net_salary', 15, 2)->default(0.00);
            $table->enum('payment_status', ['unpaid', 'paid'])->default('unpaid');
            $table->date('payment_date')->nullable();
            $table->bigInteger('payment_account_id')->unsigned()->nullable()->comment('Akun Kas/Bank sumber dana');
            $table->bigInteger('journal_entry_id')->unsigned()->nullable()->comment('Relasi ke Buku Besar (Accrual Journal)');
            $table->timestamps();

            $table->foreign('payroll_period_id')->references('id')->on('payroll_periods')->onDelete('cascade');
            $table->foreign('employee_id')->references('id')->on('employees');
            $table->foreign('branch_id')->references('id')->on('branches');
            $table->foreign('payment_account_id')->references('id')->on('accounts');
            $table->foreign('journal_entry_id')->references('id')->on('journal_entries')->onDelete('set null');
        });
    }

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