<?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('attendance_settings', function (Blueprint $table) {
            if (!Schema::hasColumn('attendance_settings', 'work_start_time')) {
                $table->time('work_start_time')->nullable()->after('radius_meters');
            }

            if (!Schema::hasColumn('attendance_settings', 'work_end_time')) {
                $table->time('work_end_time')->nullable()->after('work_start_time');
            }

            if (!Schema::hasColumn('attendance_settings', 'late_tolerance_minutes')) {
                $table->integer('late_tolerance_minutes')->default(0)->after('work_end_time');
            }

            if (!Schema::hasColumn('attendance_settings', 'early_leave_tolerance_minutes')) {
                $table->integer('early_leave_tolerance_minutes')->default(0)->after('late_tolerance_minutes');
            }
        });
    }

    public function down(): void
    {
        Schema::table('attendance_settings', function (Blueprint $table) {
            if (Schema::hasColumn('attendance_settings', 'early_leave_tolerance_minutes')) {
                $table->dropColumn('early_leave_tolerance_minutes');
            }

            if (Schema::hasColumn('attendance_settings', 'late_tolerance_minutes')) {
                $table->dropColumn('late_tolerance_minutes');
            }

            if (Schema::hasColumn('attendance_settings', 'work_end_time')) {
                $table->dropColumn('work_end_time');
            }

            if (Schema::hasColumn('attendance_settings', 'work_start_time')) {
                $table->dropColumn('work_start_time');
            }
        });
    }
};