<?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_employee_snapshots', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('payroll_period_id')->constrained('payroll_periods')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->decimal('base_salary', 14, 2)->default(0);
            $table->unsignedInteger('attendance_days')->default(0);
            $table->unsignedInteger('absent_days')->default(0);
            $table->unsignedInteger('late_days')->default(0);
            $table->decimal('leave_days', 10, 2)->default(0);
            $table->unsignedInteger('overtime_minutes')->default(0);
            $table->decimal('adjustments_total', 14, 2)->default(0);
            $table->decimal('gross_amount', 14, 2)->nullable();
            $table->json('snapshot_data')->nullable();
            $table->timestamp('generated_at');
            $table->timestamps();

            $table->unique(['payroll_period_id', 'employee_id']);
            $table->index(['tenant_id', 'payroll_period_id']);
        });
    }

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