<?php

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

/**
 * SIFA P3-T3 — SOAP note amendments / addenda (spec §7 clinical_notes.amendments).
 * After a note is signed & locked, editing a SOAP section records an amendment
 * row (previous → new text + reason) so the audit trail is preserved. The
 * amendment history is viewable inline on the consult screen.
 */
class CreateClcNoteAmendmentsTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('clc_note_amendments')) return;

        Schema::create('clc_note_amendments', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('appointment_id');
            $table->enum('section', [
                'subjective', 'objective', 'assessment', 'plan', 'investigations', 'other',
            ]);
            $table->text('previous_text')->nullable();
            $table->text('new_text')->nullable();
            $table->text('reason')->nullable();
            $table->unsignedInteger('amended_by')->nullable();   // users.id
            $table->timestamp('created_at')->nullable();

            $table->foreign('business_id')->references('id')->on('business')->onDelete('cascade');
            $table->foreign('appointment_id')->references('id')->on('clc_appointments')->onDelete('cascade');
            $table->index(['business_id', 'appointment_id']);
        });
    }

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