<?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('hr_overtime_rate_policies', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_default')->default(false);
            $table->decimal('normal_multiplier', 5, 2)->default(1);
            $table->decimal('weekend_multiplier', 5, 2)->default(1.5);
            $table->decimal('holiday_multiplier', 5, 2)->default(2);
            $table->decimal('night_multiplier', 5, 2)->default(1.5);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->index(['tenant_id', 'is_default', 'is_active']);
        });
        Schema::table('overtime_requests', function (Blueprint $table): void {
            $table->string('rate_category', 20)->default('normal');
            $table->decimal('applied_multiplier', 5, 2)->nullable();
        });
        Schema::create('hr_salary_structures', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->string('currency', 3)->default('USD');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });
        Schema::create('hr_salary_structure_components', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('structure_id')->constrained('hr_salary_structures')->cascadeOnDelete();
            $table->string('component_type', 20);
            $table->string('name');
            $table->string('calculation', 20);
            $table->decimal('amount', 14, 2);
            $table->boolean('is_taxable')->default(false);
            $table->timestamps();
        });
        Schema::table('employees', function (Blueprint $table): void {
            $table->foreignId('salary_structure_id')->nullable()
                ->constrained('hr_salary_structures')->nullOnDelete();
            $table->string('payment_method')->nullable();
            $table->string('currency', 3)->nullable();
            $table->string('payroll_group')->nullable();
            $table->string('insurance_provider')->nullable();
        });
    }

    public function down(): void
    {
        Schema::table('employees', fn (Blueprint $t) => $t->dropConstrainedForeignId('salary_structure_id'));
        Schema::table('employees', fn (Blueprint $t) => $t->dropColumn([
            'payment_method', 'currency', 'payroll_group', 'insurance_provider',
        ]));
        Schema::dropIfExists('hr_salary_structure_components');
        Schema::dropIfExists('hr_salary_structures');
        Schema::table('overtime_requests', fn (Blueprint $t) => $t->dropColumn([
            'rate_category', 'applied_multiplier',
        ]));
        Schema::dropIfExists('hr_overtime_rate_policies');
    }
};
