<?php

declare(strict_types=1);

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('host_system_update_checks')) {
            return;
        }

        Schema::table('host_system_update_checks', function (Blueprint $table): void {
            if (! Schema::hasColumn('host_system_update_checks', 'force_update')) {
                $table->boolean('force_update')->default(false)->after('requirements')->comment('是否为强制更新');
            }

            if (! Schema::hasColumn('host_system_update_checks', 'min_upgradable_update_version')) {
                $table->unsignedInteger('min_upgradable_update_version')
                    ->nullable()
                    ->after('min_upgradable_version')
                    ->comment('最低可升级内部号');
            }
        });
    }

    public function down(): void
    {
        if (! Schema::hasTable('host_system_update_checks')) {
            return;
        }

        Schema::table('host_system_update_checks', function (Blueprint $table): void {
            $columns = array_values(array_filter([
                Schema::hasColumn('host_system_update_checks', 'force_update') ? 'force_update' : null,
                Schema::hasColumn('host_system_update_checks', 'min_upgradable_update_version')
                    ? 'min_upgradable_update_version'
                    : null,
            ]));

            if ($columns !== []) {
                $table->dropColumn($columns);
            }
        });
    }
};
