<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Fitur:
     * 1. Setting hari libur mingguan (weekend_days)
     * 2. Tabel tanggal merah/libur nasional
     * 3. Setting alarm reminder
     * 4. Sistem overtime/lembur dengan approval
     */
    public function up(): void
    {
        // 1. Tambah setting hari libur dan alarm ke companies
        Schema::table('companies', function (Blueprint $table) {
            // Hari libur mingguan (JSON array: [0=Minggu, 6=Sabtu] => default: ["0","6"])
            $table->json('weekend_days')->nullable()->after('late_tolerance_minutes');

            // Alarm settings
            $table->boolean('alarm_enabled')->default(true)->after('weekend_days');
            $table->time('checkin_reminder_time')->nullable()->after('alarm_enabled'); // Jam reminder checkin
            $table->integer('checkout_reminder_after_hours')->default(9)->after('checkin_reminder_time'); // Reminder checkout setelah X jam dari checkin

            // Overtime settings
            $table->boolean('overtime_enabled')->default(true)->after('checkout_reminder_after_hours');
            $table->boolean('overtime_auto_approval')->default(false)->after('overtime_enabled'); // Auto approve overtime
            $table->integer('minimum_overtime_minutes')->default(30)->after('overtime_auto_approval'); // Minimal lembur untuk di-klaim
        });

        // 2. Tabel tanggal merah / libur nasional
        Schema::create('holidays', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->nullable()->constrained()->onDelete('cascade'); // null = berlaku untuk semua
            $table->date('date');
            $table->string('name'); // Nama hari libur (misal: Hari Raya Idul Fitri)
            $table->text('description')->nullable();
            $table->boolean('is_recurring')->default(false); // Apakah berulang tiap tahun
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index(['company_id', 'date']);
        });

        // 3. Tabel overtime/lembur
        Schema::create('overtimes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('company_id')->constrained()->onDelete('cascade');
            $table->foreignId('attendance_id')->nullable()->constrained()->onDelete('set null');
            $table->date('date');
            $table->time('start_time'); // Jam mulai lembur
            $table->time('end_time'); // Jam selesai lembur
            $table->integer('duration_minutes'); // Durasi dalam menit
            $table->enum('type', ['weekday', 'weekend', 'holiday'])->default('weekday'); // Tipe lembur
            $table->text('reason')->nullable(); // Alasan lembur
            $table->text('notes')->nullable();
            $table->enum('status', ['pending', 'approved', 'rejected', 'cancelled'])->default('pending');
            $table->foreignId('approved_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamp('approved_at')->nullable();
            $table->text('rejection_reason')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'date']);
            $table->index(['company_id', 'status']);
        });
    }

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

        Schema::table('companies', function (Blueprint $table) {
            $table->dropColumn([
                'weekend_days',
                'alarm_enabled',
                'checkin_reminder_time',
                'checkout_reminder_after_hours',
                'overtime_enabled',
                'overtime_auto_approval',
                'minimum_overtime_minutes',
            ]);
        });
    }
};
