<?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
    {
        Schema::create('operational_control_events', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('channel', 32);
            $table->string('category', 64);
            $table->string('severity', 16);
            $table->string('event_code', 64);
            $table->string('summary', 512)->nullable();
            $table->string('correlation_id', 64)->nullable();
            $table->string('reference_type', 64)->nullable();
            $table->unsignedBigInteger('reference_id')->nullable();
            $table->json('meta')->nullable();
            $table->timestamp('acknowledged_at')->nullable();
            $table->foreignId('acknowledged_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['tenant_id', 'category', 'created_at'], 'occ_tenant_cat_created');
            $table->index(['tenant_id', 'severity', 'created_at'], 'occ_tenant_sev_created');
            $table->index(['tenant_id', 'acknowledged_at', 'created_at'], 'occ_tenant_ack_created');
        });
    }

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