<?php

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

/**
 * lab_communication_logs — protocol-level event log. Raw device messages are
 * kept here so the parser can be tuned against real captured traffic.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('lab_communication_logs')) {
            return;
        }

        Schema::create('lab_communication_logs', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('agent_id')->nullable()->index();
            $table->unsignedInteger('device_id')->nullable()->index();
            $table->string('vendor', 30)->nullable()->index();
            $table->string('level', 10)->default('info')->comment('info, success, warning, error');
            $table->string('category', 20)->default('system')->comment('connection, worklist, result, error, sync, system');
            $table->enum('direction', ['in', 'out'])->nullable();
            $table->string('device_ip', 45)->nullable();
            $table->string('sample_no', 30)->nullable();
            $table->text('message');
            $table->text('raw_message')->nullable()->comment('Raw bytes (hex) of the device frame');
            $table->json('context')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('created_at');
            $table->index('level');
            $table->index('category');
        });
    }

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