<?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('witness_statement_exhibits', function (Blueprint $table) {
            $table->id();
            $table->foreignId('solicitor_id')->constrained('solicitors')->cascadeOnDelete();
            $table->foreignId('witness_statement_id')->constrained('witness_statements')->cascadeOnDelete();
            // The confirmed finding this exhibit was promoted from (optional).
            $table->foreignId('dsar_evidence_item_id')->nullable()->constrained('dsar_evidence_items')->nullOnDelete();
            // The source document + page being exhibited.
            $table->foreignId('document_id')->nullable()->constrained('documents')->nullOnDelete();
            $table->unsignedSmallInteger('page')->nullable();
            $table->string('exhibit_ref', 16)->nullable(); // e.g. "MO/1"
            $table->string('label')->nullable();
            $table->foreignId('added_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            // Adding the same document page twice is a no-op (idempotent add/remove).
            $table->unique(['witness_statement_id', 'document_id', 'page'], 'wse_unique_page');
        });
    }

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