<?php

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

/**
 * Full MAR + nursing documentation on care units.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('care_mar_orders', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('visit_id')->constrained('care_visits')->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->foreignId('prescription_id')->constrained('care_prescriptions')->cascadeOnDelete();
            $table->foreignId('prescription_item_id')->constrained('care_prescription_items')->cascadeOnDelete();
            $table->string('drug_name');
            $table->string('dosage')->nullable();
            $table->string('route')->nullable();
            /** od|bd|tds|qds|q4h|q6h|q8h|prn|stat|custom */
            $table->string('frequency_code')->default('od');
            $table->json('times_of_day')->nullable();
            $table->boolean('is_prn')->default(false);
            /** active|paused|stopped */
            $table->string('status')->default('active');
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->text('instructions')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['prescription_item_id']);
            $table->index(['visit_id', 'status']);
            $table->index(['organization_id', 'status']);
        });

        Schema::create('care_mar_administrations', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('mar_order_id')->constrained('care_mar_orders')->cascadeOnDelete();
            $table->foreignId('visit_id')->constrained('care_visits')->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->foreignId('care_unit_id')->nullable()->constrained('care_care_units')->nullOnDelete();
            $table->timestamp('scheduled_for')->nullable();
            $table->timestamp('recorded_at');
            /** given|held|refused|missed */
            $table->string('status');
            $table->string('dose_given')->nullable();
            $table->string('route')->nullable();
            $table->string('administered_by')->nullable();
            $table->string('witness_by')->nullable();
            $table->string('reason')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index(['visit_id', 'recorded_at']);
            $table->index(['care_unit_id', 'scheduled_for']);
            $table->index(['mar_order_id', 'status']);
        });

        Schema::create('care_nursing_notes', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('visit_id')->constrained('care_visits')->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->foreignId('care_unit_id')->nullable()->constrained('care_care_units')->nullOnDelete();
            /** progress|assessment|intervention|observation|other */
            $table->string('note_type')->default('progress');
            $table->string('shift_code')->nullable();
            $table->text('body');
            $table->string('recorded_by')->nullable();
            $table->timestamp('recorded_at');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['visit_id', 'recorded_at']);
            $table->index(['care_unit_id', 'recorded_at']);
        });

        Schema::create('care_ward_handovers', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('care_unit_id')->constrained('care_care_units')->cascadeOnDelete();
            $table->string('from_shift')->nullable();
            $table->string('to_shift')->nullable();
            $table->timestamp('handed_over_at')->nullable();
            $table->string('handed_over_by')->nullable();
            $table->string('received_by')->nullable();
            $table->text('situation')->nullable();
            $table->text('background')->nullable();
            $table->text('assessment')->nullable();
            $table->text('recommendation')->nullable();
            $table->json('patient_summaries')->nullable();
            /** draft|completed */
            $table->string('status')->default('draft');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['care_unit_id', 'status']);
            $table->index(['care_unit_id', 'handed_over_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('care_ward_handovers');
        Schema::dropIfExists('care_nursing_notes');
        Schema::dropIfExists('care_mar_administrations');
        Schema::dropIfExists('care_mar_orders');
    }
};
