<?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_answers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('solicitor_id')->constrained('solicitors')->cascadeOnDelete();
            $table->foreignId('witness_statement_id')->constrained('witness_statements')->cascadeOnDelete();
            $table->string('section_key', 64);
            $table->string('question_key', 64);
            // Uniform value store: scalar, array (multiselect) or repeater rows.
            $table->json('value')->nullable();
            // Where the answer came from.
            $table->string('origin', 24)->default('user'); // client_data|claim_data|evidence|user|previous
            // Lifecycle of a suggestion / edit.
            $table->string('review_status', 16)->default('manual'); // manual|suggested|accepted|edited|rejected
            // For evidence-sourced answers: {dsar_evidence_item_id, document_id, page, excerpt, severity, care_setting_id}.
            $table->json('provenance')->nullable();
            $table->foreignId('answered_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['witness_statement_id', 'section_key', 'question_key'], 'wsa_unique_question');
            $table->index(['witness_statement_id', 'review_status'], 'wsa_stmt_review_idx');
        });
    }

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