<?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('care_investigation_types', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->string('name');
            $table->string('code')->nullable();
            $table->string('category')->default('blood'); // blood, urine, stool, xray, ultrasound, ct, mri, ecg, custom
            $table->text('description')->nullable();
            $table->string('unit')->nullable();
            $table->decimal('reference_low', 12, 4)->nullable();
            $table->decimal('reference_high', 12, 4)->nullable();
            $table->string('reference_text')->nullable();
            $table->unsignedInteger('price_minor')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
            $table->index(['organization_id', 'category']);
        });

        Schema::create('care_investigation_requests', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained('care_branches')->cascadeOnDelete();
            $table->foreignId('visit_id')->constrained('care_visits')->cascadeOnDelete();
            $table->foreignId('consultation_id')->nullable()->constrained('care_consultations')->nullOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->foreignId('investigation_type_id')->constrained('care_investigation_types')->cascadeOnDelete();
            $table->foreignId('practitioner_id')->nullable()->constrained('care_practitioners')->nullOnDelete();
            $table->string('status')->default('pending');
            $table->string('priority')->default('routine'); // routine, urgent
            $table->text('clinical_notes')->nullable();
            $table->string('requested_by')->nullable();
            $table->foreignId('assigned_member_id')->nullable()->constrained('care_members')->nullOnDelete();
            $table->string('sample_barcode')->nullable();
            $table->timestamp('sample_collected_at')->nullable();
            $table->string('sample_collected_by')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamp('delivered_at')->nullable();
            $table->string('approved_by')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['branch_id', 'status']);
            $table->index(['patient_id', 'status']);
        });

        Schema::create('care_investigation_results', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('investigation_request_id')->constrained('care_investigation_requests')->cascadeOnDelete();
            $table->text('result_summary')->nullable();
            $table->text('interpretation')->nullable();
            $table->boolean('is_abnormal')->default(false);
            $table->string('status')->default('draft'); // draft, approved
            $table->string('entered_by')->nullable();
            $table->string('approved_by')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
        });

        Schema::create('care_investigation_result_values', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('investigation_result_id')->constrained('care_investigation_results')->cascadeOnDelete();
            $table->string('parameter');
            $table->string('value')->nullable();
            $table->string('unit')->nullable();
            $table->decimal('reference_low', 12, 4)->nullable();
            $table->decimal('reference_high', 12, 4)->nullable();
            $table->string('reference_text')->nullable();
            $table->boolean('is_abnormal')->default(false);
            $table->timestamps();
        });

        Schema::create('care_investigation_attachments', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('investigation_result_id')->constrained('care_investigation_results')->cascadeOnDelete();
            $table->string('file_path');
            $table->string('original_name');
            $table->string('mime_type')->nullable();
            $table->string('uploaded_by')->nullable();
            $table->timestamps();
        });

        Schema::create('care_prescriptions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('visit_id')->constrained('care_visits')->cascadeOnDelete();
            $table->foreignId('consultation_id')->nullable()->constrained('care_consultations')->nullOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->foreignId('practitioner_id')->nullable()->constrained('care_practitioners')->nullOnDelete();
            $table->string('status')->default('draft'); // draft, active, dispensed, cancelled
            $table->text('notes')->nullable();
            $table->string('prescribed_by')->nullable();
            $table->string('dispensed_by')->nullable();
            $table->timestamp('dispensed_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['patient_id', 'status']);
        });

        Schema::create('care_prescription_items', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('prescription_id')->constrained('care_prescriptions')->cascadeOnDelete();
            $table->boolean('is_procedure')->default(false);
            $table->string('name');
            $table->string('dosage')->nullable();
            $table->string('frequency')->nullable();
            $table->string('duration')->nullable();
            $table->string('route')->nullable();
            $table->string('quantity')->nullable();
            $table->text('instructions')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('care_prescription_items');
        Schema::dropIfExists('care_prescriptions');
        Schema::dropIfExists('care_investigation_attachments');
        Schema::dropIfExists('care_investigation_result_values');
        Schema::dropIfExists('care_investigation_results');
        Schema::dropIfExists('care_investigation_requests');
        Schema::dropIfExists('care_investigation_types');
    }
};
