<?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('security_events', function (Blueprint $table) {
            $table->id();
            $table->enum('severity', ['low', 'medium', 'high', 'critical'])->default('medium');
            $table->string('event_type'); // replay_attack, brute_force, invalid_signature, tamper_detected, etc.
            $table->string('license_key')->nullable();
            $table->foreignId('license_id')->nullable()->constrained()->nullOnDelete();
            $table->string('ip_address', 45)->nullable();
            $table->string('domain')->nullable();
            $table->text('description');
            $table->json('payload')->nullable(); // sanitized request data
            $table->boolean('is_resolved')->default(false);
            $table->timestamp('resolved_at')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['event_type', 'created_at']);
            $table->index(['severity', 'is_resolved']);
            $table->index('ip_address');
            $table->index('license_key');
        });
    }

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