<?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('broker_accounts', function (Blueprint $table) {
            $table->string('api_key')->nullable()->after('broker');
            $table->text('api_secret')->nullable()->after('api_key');
            $table->string('act_id')->nullable()->after('client_id');
        });

        Schema::create('system_logs', function (Blueprint $table) {
            $table->id();
            $table->string('channel', 50)->default('system');
            $table->string('level', 20)->default('info');
            $table->string('message');
            $table->json('context')->nullable();
            $table->timestamps();
            $table->index(['channel', 'created_at']);
        });

        Schema::create('bot_runtime_state', function (Blueprint $table) {
            $table->id();
            $table->string('worker', 50)->unique();
            $table->string('status', 30)->default('stopped');
            $table->timestamp('last_heartbeat_at')->nullable();
            $table->json('meta')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('bot_runtime_state');
        Schema::dropIfExists('system_logs');

        Schema::table('broker_accounts', function (Blueprint $table) {
            $table->dropColumn(['api_key', 'api_secret', 'act_id']);
        });
    }
};
