<?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('fingerprint_machines', function (Blueprint $table) {
            if (!Schema::hasColumn('fingerprint_machines', 'connector_host')) {
                $table->string('connector_host', 100)->nullable()->after('cloud_api_token');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'connector_port')) {
                $table->string('connector_port', 10)->default('8000')->after('connector_host');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'connector_token')) {
                $table->text('connector_token')->nullable()->after('connector_port');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'last_seen_at')) {
                $table->timestamp('last_seen_at')->nullable()->after('last_sync');
            }
        });
    }

    public function down(): void
    {
        Schema::table('fingerprint_machines', function (Blueprint $table) {
            $columns = ['connector_host', 'connector_port', 'connector_token', 'last_seen_at'];
            foreach ($columns as $column) {
                if (Schema::hasColumn('fingerprint_machines', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
