<?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_components', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('employee_id')->unsigned();
            $table->bigInteger('payroll_component_id')->unsigned();
            $table->decimal('amount', 15, 2)->default(0.00);
            $table->timestamps();

            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('payroll_component_id')->references('id')->on('payroll_components')->onDelete('cascade');
            
            // Cegah duplikasi komponen yang sama untuk satu karyawan
            $table->unique(['employee_id', 'payroll_component_id'], 'idx_emp_comp');
        });
    }

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