<?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('staff', function (Blueprint $table) {
            if (! Schema::hasColumn('staff', 'nfc_uid')) {
                $table->string('nfc_uid', 100)->nullable()->unique()->after('employee_code');
            }
            if (! Schema::hasColumn('staff', 'office_start_time')) {
                $table->time('office_start_time')->nullable()->after('join_date');
            }
            if (! Schema::hasColumn('staff', 'office_end_time')) {
                $table->time('office_end_time')->nullable()->after('office_start_time');
            }
            if (! Schema::hasColumn('staff', 'late_grace_minutes')) {
                $table->unsignedInteger('late_grace_minutes')->default(15)->after('office_end_time');
            }
        });

        Schema::table('staff_attendances', function (Blueprint $table) {
            if (! Schema::hasColumn('staff_attendances', 'nfc_uid')) {
                $table->string('nfc_uid', 100)->nullable()->after('check_out');
            }
            if (! Schema::hasColumn('staff_attendances', 'late_minutes')) {
                $table->unsignedInteger('late_minutes')->default(0)->after('nfc_uid');
            }
            if (! Schema::hasColumn('staff_attendances', 'early_exit_minutes')) {
                $table->unsignedInteger('early_exit_minutes')->default(0)->after('late_minutes');
            }
            if (! Schema::hasColumn('staff_attendances', 'overtime_minutes')) {
                $table->unsignedInteger('overtime_minutes')->default(0)->after('early_exit_minutes');
            }
            if (! Schema::hasColumn('staff_attendances', 'is_manual')) {
                $table->boolean('is_manual')->default(false)->after('overtime_minutes');
            }
            if (! Schema::hasColumn('staff_attendances', 'manual_reason')) {
                $table->string('manual_reason')->nullable()->after('is_manual');
            }
            if (! Schema::hasColumn('staff_attendances', 'is_missed_punch_adjusted')) {
                $table->boolean('is_missed_punch_adjusted')->default(false)->after('manual_reason');
            }
            if (! Schema::hasColumn('staff_attendances', 'first_punch_at')) {
                $table->dateTime('first_punch_at')->nullable()->after('is_missed_punch_adjusted');
            }
            if (! Schema::hasColumn('staff_attendances', 'last_punch_at')) {
                $table->dateTime('last_punch_at')->nullable()->after('first_punch_at');
            }
            $table->index(['attendance_date', 'status'], 'staff_attendance_date_status_idx');
        });

        if (! Schema::hasTable('attendance_holidays')) {
            Schema::create('attendance_holidays', function (Blueprint $table) {
                $table->id();
                $table->date('holiday_date')->unique();
                $table->string('title', 120);
                $table->text('notes')->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('attendance_holidays')) {
            Schema::dropIfExists('attendance_holidays');
        }

        Schema::table('staff_attendances', function (Blueprint $table) {
            if (Schema::hasColumn('staff_attendances', 'last_punch_at')) {
                $table->dropColumn('last_punch_at');
            }
            if (Schema::hasColumn('staff_attendances', 'first_punch_at')) {
                $table->dropColumn('first_punch_at');
            }
            if (Schema::hasColumn('staff_attendances', 'is_missed_punch_adjusted')) {
                $table->dropColumn('is_missed_punch_adjusted');
            }
            if (Schema::hasColumn('staff_attendances', 'manual_reason')) {
                $table->dropColumn('manual_reason');
            }
            if (Schema::hasColumn('staff_attendances', 'is_manual')) {
                $table->dropColumn('is_manual');
            }
            if (Schema::hasColumn('staff_attendances', 'overtime_minutes')) {
                $table->dropColumn('overtime_minutes');
            }
            if (Schema::hasColumn('staff_attendances', 'early_exit_minutes')) {
                $table->dropColumn('early_exit_minutes');
            }
            if (Schema::hasColumn('staff_attendances', 'late_minutes')) {
                $table->dropColumn('late_minutes');
            }
            if (Schema::hasColumn('staff_attendances', 'nfc_uid')) {
                $table->dropColumn('nfc_uid');
            }
        });

        Schema::table('staff', function (Blueprint $table) {
            if (Schema::hasColumn('staff', 'late_grace_minutes')) {
                $table->dropColumn('late_grace_minutes');
            }
            if (Schema::hasColumn('staff', 'office_end_time')) {
                $table->dropColumn('office_end_time');
            }
            if (Schema::hasColumn('staff', 'office_start_time')) {
                $table->dropColumn('office_start_time');
            }
            if (Schema::hasColumn('staff', 'nfc_uid')) {
                $table->dropUnique(['nfc_uid']);
                $table->dropColumn('nfc_uid');
            }
        });
    }
};
