<?php

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

/**
 * PR 5 — clinical pathway catalog + patient pathway activation.
 * Pathway bindings use template_code only (no template_id FK).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('care_clinical_pathways', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('code')->unique();
            $table->string('name');
            $table->text('description')->nullable();
            $table->json('match_rules')->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->json('meta')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['is_active', 'sort_order']);
        });

        Schema::create('care_pathway_templates', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pathway_id')->constrained('care_clinical_pathways')->cascadeOnDelete();
            $table->string('template_code');
            $table->boolean('is_required_on_activation')->default(false);
            $table->string('phase')->default('any'); // acute, follow_up, any
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();

            $table->unique(['pathway_id', 'template_code']);
            $table->index(['template_code']);
        });

        Schema::create('care_patient_pathways', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->foreignId('pathway_id')->constrained('care_clinical_pathways')->restrictOnDelete();
            $table->string('status')->default('active'); // active, resolved, inactive
            $table->timestamp('activated_at');
            $table->string('activated_by')->nullable();
            $table->foreignId('activation_consultation_id')->nullable()->constrained('care_consultations')->nullOnDelete();
            $table->string('activation_diagnosis_text')->nullable();
            $table->timestamp('resolved_at')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['patient_id', 'status']);
            $table->index(['patient_id', 'pathway_id', 'status']);
        });

        Schema::table('care_assessments', function (Blueprint $table) {
            $table->foreignId('patient_pathway_id')
                ->nullable()
                ->after('visit_id')
                ->constrained('care_patient_pathways')
                ->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('care_assessments', function (Blueprint $table) {
            $table->dropConstrainedForeignId('patient_pathway_id');
        });
        Schema::dropIfExists('care_patient_pathways');
        Schema::dropIfExists('care_pathway_templates');
        Schema::dropIfExists('care_clinical_pathways');
    }
};
