<?php

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

/**
 * Adds support for two server monitoring modes:
 *   - `agent`  : Agent installed on the box. Pushes full metrics every N seconds.
 *   - `manual` : No agent. Backend pings the host on a schedule. Reports up/down
 *                only — CPU/RAM/disk are unknown.
 *
 * `check_interval_seconds` controls how often the backend pings manual servers,
 * and how often the dashboard refreshes UI for both kinds.
 */
return new class extends Migration {
    public function up(): void
    {
        Schema::table('servers', function (Blueprint $table) {
            $table->enum('monitoring_mode', ['agent', 'manual'])
                ->default('agent')
                ->after('type')
                ->comment('agent = full metrics via on-host script; manual = up/down only via remote ping');

            $table->unsignedInteger('check_interval_seconds')
                ->default(5)
                ->after('monitoring_mode')
                ->comment('How often to refresh status. Agent default 30s, manual default 60s.');

            $table->enum('manual_check_method', ['icmp', 'tcp', 'http', 'https'])
                ->default('icmp')
                ->after('check_interval_seconds')
                ->comment('How to ping when monitoring_mode = manual');

            $table->unsignedSmallInteger('manual_check_port')
                ->nullable()
                ->after('manual_check_method')
                ->comment('Port to probe for tcp/http checks');

            $table->string('manual_check_path', 255)
                ->nullable()
                ->after('manual_check_port')
                ->comment('URL path for http(s) probes');

            $table->unsignedSmallInteger('manual_timeout_seconds')
                ->default(5)
                ->after('manual_check_path');
        });
    }

    public function down(): void
    {
        Schema::table('servers', function (Blueprint $table) {
            $table->dropColumn([
                'monitoring_mode', 'check_interval_seconds',
                'manual_check_method', 'manual_check_port', 'manual_check_path',
                'manual_timeout_seconds',
            ]);
        });
    }
};
