<?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_runs', function (Blueprint $table) {
            $table->id();
            $table->string('period')->unique();          // 'YYYY-MM'
            $table->date('period_start');
            $table->date('period_end');
            $table->date('pay_date')->nullable();
            $table->date('effective_date');               // date used for effective-dated lookups

            // DRAFT | CALCULATED | APPROVED | PAID | CLOSED
            $table->string('status', 16)->default('DRAFT');

            $table->string('statutory_ruleset')->nullable(); // e.g. PITA
            $table->string('salary_structure')->nullable();  // optional: run scoped to one structure

            // Totals (populated on calculate)
            $table->decimal('total_gross', 18, 2)->default(0);
            $table->decimal('total_statutory', 18, 2)->default(0);
            $table->decimal('total_voluntary', 18, 2)->default(0);
            $table->decimal('total_deductions', 18, 2)->default(0);
            $table->decimal('total_net', 18, 2)->default(0);
            $table->unsignedInteger('employee_count')->default(0);

            $table->timestamp('calculated_at')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();

            $table->text('notes')->nullable();
            $table->timestamps();
        });
    }

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