<?php

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

/**
 * lab_device_errors — errors reported by an analyzer, surfaced on the device
 * monitor until acknowledged.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('lab_device_errors')) {
            return;
        }

        Schema::create('lab_device_errors', 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('error_code', 30)->nullable();
            $table->date('error_date')->nullable();
            $table->time('error_time')->nullable();
            $table->text('message')->nullable();
            $table->json('added_info')->nullable();
            $table->string('device_ip', 45)->nullable();
            $table->boolean('acknowledged')->default(false);
            $table->timestamp('acknowledged_at')->nullable();
            $table->unsignedBigInteger('acknowledged_by')->nullable();
            $table->timestamps();

            $table->index('acknowledged');
        });
    }

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