<?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', 'connection_type')) {
                $table->string('connection_type', 20)->default('local')->after('port');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'protocol')) {
                $table->string('protocol', 30)->default('fingerspot_local')->after('connection_type');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'machine_number')) {
                $table->string('machine_number', 10)->default('1')->after('protocol');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'password')) {
                $table->text('password')->nullable()->after('machine_number');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'cloud_id')) {
                $table->string('cloud_id', 100)->nullable()->after('password');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'cloud_api_token')) {
                $table->text('cloud_api_token')->nullable()->after('cloud_id');
            }
            if (!Schema::hasColumn('fingerprint_machines', 'device_info')) {
                $table->json('device_info')->nullable()->after('cloud_api_token');
            }
        });
    }

    public function down(): void
    {
        Schema::table('fingerprint_machines', function (Blueprint $table) {
            $columns = ['connection_type', 'protocol', 'machine_number', 'password', 'cloud_id', 'cloud_api_token', 'device_info'];
            foreach ($columns as $column) {
                if (Schema::hasColumn('fingerprint_machines', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
