<?php

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

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

        Schema::table('finance_settings', function (Blueprint $table) {
            if (!Schema::hasColumn('finance_settings', 'last_health_check_at')) {
                $table->timestamp('last_health_check_at')->nullable()->after('last_response_time_ms');
            }

            if (!Schema::hasColumn('finance_settings', 'health_report')) {
                $table->longText('health_report')->nullable()->after('last_health_check_at');
            }
        });
    }

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

        Schema::table('finance_settings', function (Blueprint $table) {
            foreach (['health_report', 'last_health_check_at'] as $column) {
                if (Schema::hasColumn('finance_settings', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
