<?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('employee_tax_assignments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('tax_id')->constrained('taxes')->cascadeOnDelete();
            $table->decimal('fixed_amount', 12, 2)->nullable();
            $table->date('effective_from');
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->timestamps();

            // Unique composite index for active assignments
            $table->unique(['employee_id', 'status'], 'emp_tax_active_unique')->where('status', 'active');
        });
    }

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