<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('slips_gaji', function (Blueprint $table) {
            $table->string('id_slip_gaji', 20)->primary(); // SG-YYYYMM-XXXX
            $table->string('id_user', 7);
            $table->string('periode', 7); // Format: YYYY-MM
            $table->date('tanggal_bayar')->nullable();
            
            // Performa pengiriman & absensi
            $table->integer('total_qty_3kg')->default(0);
            $table->integer('total_qty_12kg')->default(0);
            $table->integer('total_hari_kerja')->default(0);

            // Rincian Keuangan
            $table->decimal('gaji_pokok_nominal', 12, 2);
            $table->decimal('insentif_nominal', 12, 2);
            $table->decimal('uang_makan_nominal', 12, 2);
            $table->decimal('potongan', 12, 2)->default(0);
            $table->decimal('total_diterima', 12, 2);

            $table->enum('status', ['draft', 'dibayar'])->default('draft');
            $table->string('bukti_transfer')->nullable();
            $table->text('catatan')->nullable();
            $table->timestamps();

            // Constraint
            $table->foreign('id_user')->references('id_user')->on('users')->onDelete('cascade');
            $table->unique(['id_user', 'periode']); // Cegah duplikasi slip gaji per periode
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('slips_gaji');
    }
};
