<?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('import_events', function (Blueprint $table) {
            $table->id();
            $table->foreignId('import_id')->constrained('imports')->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();

            // Event details
            $table->string('event', 50); // created|parsing_started|parsed|validating|importing|completed|failed|retry|...
            $table->string('level', 10)->default('info'); // info|warning|error
            $table->text('message')->nullable();
            $table->json('context')->nullable();

            // An optional pointer to a specific row related to the event
            $table->foreignId('import_row_id')->nullable()->constrained('import_rows')->nullOnDelete();

            // Indexes
            $table->index(['import_id', 'event']);
            $table->index(['import_id', 'level']);
            $table->index('user_id');

            $table->timestamps();
        });
    }

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