<?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('synapse_runs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('solicitor_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('agent_type'); // 'dsar_identifier' | 'document_reviewer'
            $table->string('subject_type')->nullable();
            $table->unsignedBigInteger('subject_id')->nullable();
            $table->string('status')->default('completed'); // 'completed' | 'failed'
            $table->text('error')->nullable();
            $table->timestamps();

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

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