<?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::table('publication_logs', function (Blueprint $table): void {
            $table->foreignId('triggered_by')->nullable()->after('publication_target_id')->constrained('users')->nullOnDelete();
            $table->unsignedTinyInteger('attempt')->default(1)->after('event');
            $table->json('portal_response')->nullable()->after('context');

            $table->index(['triggered_by', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::table('publication_logs', function (Blueprint $table): void {
            $table->dropForeign(['triggered_by']);
            $table->dropColumn(['triggered_by', 'attempt', 'portal_response']);
        });
    }
};
