<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/** PR 4 — materialize instrument scores on assessment complete. */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('care_assessment_scores', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('assessment_id')->constrained('care_assessments')->cascadeOnDelete();
            $table->string('template_code')->index();
            $table->decimal('total_score', 12, 4)->nullable();
            $table->decimal('max_score', 12, 4)->nullable();
            $table->json('subscores')->nullable();
            $table->string('severity_label')->nullable();
            $table->timestamp('computed_at');
            $table->timestamps();

            $table->unique('assessment_id');
            $table->index(['owner_ref', 'template_code', 'computed_at']);
        });
    }

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