<?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_holidays', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->date('date');
            $table->boolean('is_recurring_yearly')->default(false);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->index(['tenant_id', 'date', 'is_active']);
        });
        Schema::create('hr_attendance_policies', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->unique()->constrained()->cascadeOnDelete();
            $table->unsignedSmallInteger('early_leave_grace_minutes')->default(0);
            $table->unsignedSmallInteger('late_grace_minutes')->nullable();
            $table->boolean('enable_early_leave_detection')->default(false);
            $table->boolean('enable_ip_restriction')->default(false);
            $table->json('allowed_ips')->nullable();
            $table->boolean('ramadan_enabled')->default(false);
            $table->date('ramadan_start_date')->nullable();
            $table->date('ramadan_end_date')->nullable();
            $table->unsignedSmallInteger('ramadan_work_minutes')->nullable();
            $table->boolean('flexible_hours_enabled')->default(false);
            $table->text('hybrid_policy_notes')->nullable();
            $table->text('remote_policy_notes')->nullable();
            $table->timestamps();
        });
        Schema::create('attendance_imports', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('original_name');
            $table->string('status', 20)->default('preview');
            $table->unsignedInteger('total_rows')->default(0);
            $table->unsignedInteger('valid_rows')->default(0);
            $table->unsignedInteger('error_rows')->default(0);
            $table->timestamp('committed_at')->nullable();
            $table->timestamps();
        });
        Schema::create('attendance_import_rows', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('attendance_import_id')->constrained('attendance_imports')->cascadeOnDelete();
            $table->unsignedInteger('row_number');
            $table->string('employee_code')->nullable();
            $table->date('attendance_date')->nullable();
            $table->time('check_in')->nullable();
            $table->time('check_out')->nullable();
            $table->json('errors')->nullable();
            $table->boolean('is_valid')->default(false);
            $table->timestamps();
            $table->unique(['attendance_import_id', 'row_number']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('attendance_import_rows');
        Schema::dropIfExists('attendance_imports');
        Schema::dropIfExists('hr_attendance_policies');
        Schema::dropIfExists('hr_holidays');
    }
};
