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

                $table->string('event', 50);
                $table->string('level', 10)->default('info');
                $table->text('message')->nullable();
                $table->json('context')->nullable();

                $table->foreignId('import_row_id')->nullable()->constrained('import_rows')->nullOnDelete();

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

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

    public function down(): void
    {
        if (Schema::hasTable('import_events')) {
            Schema::drop('import_events');
        }
    }
};
