<?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
    {
        if (Schema::hasTable('nx_device_errors')) {
            return;
        }

        Schema::create('nx_device_errors', function (Blueprint $table) {
            $table->increments('id');
            $table->date('error_date');
            $table->time('error_time');
            $table->string('error_number', 5)->index()->comment('Alphanumeric error code e.g. E0110');
            $table->json('added_info')->nullable()->comment('Array of up to 9 additional info fields');
            $table->string('device_ip', 45)->nullable();
            $table->enum('comm_type', ['1', '2', '3', 'A', 'B', '4', 'C'])->nullable();
            $table->boolean('acknowledged')->default(false);
            $table->timestamp('acknowledged_at')->nullable();
            $table->timestamps();

            $table->index(['error_date', 'error_time']);
        });
    }

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