<?php

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

/**
 * lab_device_connections — runtime connection log: one row per analyzer
 * session opened against the agent.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('lab_device_connections')) {
            return;
        }

        Schema::create('lab_device_connections', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('agent_id')->nullable()->index();
            $table->unsignedInteger('device_id')->nullable()->index();
            $table->string('vendor', 30)->nullable();
            $table->string('device_ip', 45)->nullable();
            $table->unsignedInteger('device_port')->nullable();
            $table->string('comm_protocol', 40)->nullable();
            $table->enum('status', ['connected', 'disconnected'])->default('connected');
            $table->timestamp('connected_at')->nullable();
            $table->timestamp('disconnected_at')->nullable();
            $table->unsignedInteger('messages_received')->default(0);
            $table->unsignedInteger('results_stored')->default(0);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('lab_device_connections');
    }
};
