<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('event_guests', function (Blueprint $table) {
            if (!Schema::hasColumn('event_guests', 'checked_in')) {
                $table->boolean('checked_in')->default(false);
            }
            if (!Schema::hasColumn('event_guests', 'checked_in_at')) {
                $table->timestamp('checked_in_at')->nullable();
            }
            if (!Schema::hasColumn('event_guests', 'checked_in_by')) {
                $table->unsignedBigInteger('checked_in_by')->nullable();
            }
            if (!Schema::hasColumn('event_guests', 'check_in_method')) {
                $table->enum('check_in_method', ['qr_scan', 'manual_search', 'manual_entry'])->nullable();
            }
            if (!Schema::hasColumn('event_guests', 'check_in_count')) {
                $table->integer('check_in_count')->default(0);
            }
            if (!Schema::hasColumn('event_guests', 'last_check_in_at')) {
                $table->timestamp('last_check_in_at')->nullable();
            }
            if (!Schema::hasColumn('event_guests', 'scanner_notes')) {
                $table->text('scanner_notes')->nullable();
            }
            if (!Schema::hasColumn('event_guests', 'scanner_device')) {
                $table->string('scanner_device')->nullable();
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('event_guests', function (Blueprint $table) {
            $columns = [
                'checked_in',
                'checked_in_at',
                'checked_in_by',
                'check_in_method',
                'check_in_count',
                'last_check_in_at',
                'scanner_notes',
                'scanner_device'
            ];
            
            foreach ($columns as $column) {
                if (Schema::hasColumn('event_guests', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
