<?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::table('shifts', function (Blueprint $table): void {
            $table->string('type', 20)->default('fixed')->after('name');
            $table->time('flexible_start')->nullable();
            $table->time('flexible_end')->nullable();
            $table->time('split_second_start')->nullable();
            $table->time('split_second_end')->nullable();
            $table->date('seasonal_from')->nullable();
            $table->date('seasonal_to')->nullable();
        });
        Schema::create('hr_shift_rotations', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });
        Schema::create('hr_shift_rotation_days', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('rotation_id')->constrained('hr_shift_rotations')->cascadeOnDelete();
            $table->unsignedSmallInteger('day_number');
            $table->foreignId('shift_id')->nullable()->constrained('shifts')->nullOnDelete();
            $table->boolean('is_day_off')->default(false);
            $table->unique(['rotation_id', 'day_number']);
        });
        Schema::create('hr_shift_rotation_employees', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('rotation_id')->constrained('hr_shift_rotations')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->date('starts_at');
            $table->date('ends_at')->nullable();
            $table->unique(['rotation_id', 'employee_id', 'starts_at'], 'rotation_employee_start_uq');
        });
        Schema::create('hr_shift_swap_requests', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('employee_a')->constrained('employees')->cascadeOnDelete();
            $table->foreignId('employee_b')->constrained('employees')->cascadeOnDelete();
            $table->foreignId('assignment_a')->constrained('employee_shift_assignments')->cascadeOnDelete();
            $table->foreignId('assignment_b')->constrained('employee_shift_assignments')->cascadeOnDelete();
            $table->date('date');
            $table->string('status', 20)->default('pending');
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->timestamps();
            $table->index(['tenant_id', 'status']);
        });
        Schema::table('leave_types', function (Blueprint $table): void {
            $table->boolean('allow_carry_forward')->default(false);
            $table->decimal('carry_forward_max_days', 8, 2)->nullable();
            $table->boolean('allow_hourly')->default(false);
            $table->decimal('max_hourly_hours', 5, 2)->nullable();
        });
        Schema::table('leave_balances', fn (Blueprint $t) => $t->decimal('carried_forward_days', 8, 2)->default(0));
        Schema::table('leave_requests', fn (Blueprint $t) => $t->decimal('hours_requested', 5, 2)->nullable());
    }

    public function down(): void
    {
        Schema::table('leave_requests', fn (Blueprint $t) => $t->dropColumn('hours_requested'));
        Schema::table('leave_balances', fn (Blueprint $t) => $t->dropColumn('carried_forward_days'));
        Schema::table('leave_types', fn (Blueprint $t) => $t->dropColumn([
            'allow_carry_forward', 'carry_forward_max_days', 'allow_hourly', 'max_hourly_hours',
        ]));
        Schema::dropIfExists('hr_shift_swap_requests');
        Schema::dropIfExists('hr_shift_rotation_employees');
        Schema::dropIfExists('hr_shift_rotation_days');
        Schema::dropIfExists('hr_shift_rotations');
        Schema::table('shifts', fn (Blueprint $t) => $t->dropColumn([
            'type', 'flexible_start', 'flexible_end', 'split_second_start',
            'split_second_end', 'seasonal_from', 'seasonal_to',
        ]));
    }
};
