<?php

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

/**
 * P5-T4 ophthalmology deep-dive — per-visit eye exam (trend-capable). The 1:1
 * opth_patient_profiles table stays the latest-summary; this is the full
 * historical record per eye (OD/OS) so the fragment can plot IOP / VA trends
 * and the glaucoma & diabetic-retinopathy tracking works over time.
 *
 * Additive + guarded: no-ops when ClinicCore isn't installed.
 */
class CreateOpthEyeExamsTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('opth_eye_exams')) return;
        if (! Schema::hasTable('clc_patients')) return; // requires ClinicCore

        Schema::create('opth_eye_exams', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('patient_id');                 // clc_patients.id
            $table->unsignedInteger('appointment_id')->nullable(); // clc_appointments.id

            // Visual acuity — uncorrected + corrected, per eye (free text: 6/6, 20/20, CF, HM…)
            $table->string('va_od', 15)->nullable();
            $table->string('va_os', 15)->nullable();
            $table->string('va_od_corrected', 15)->nullable();
            $table->string('va_os_corrected', 15)->nullable();

            // Refraction per eye
            $table->decimal('sphere_od', 5, 2)->nullable();
            $table->decimal('sphere_os', 5, 2)->nullable();
            $table->decimal('cylinder_od', 5, 2)->nullable();
            $table->decimal('cylinder_os', 5, 2)->nullable();
            $table->unsignedSmallInteger('axis_od')->nullable();   // 0..180
            $table->unsignedSmallInteger('axis_os')->nullable();
            $table->decimal('add_od', 4, 2)->nullable();
            $table->decimal('add_os', 4, 2)->nullable();

            // Intraocular pressure (mmHg) + method
            $table->decimal('iop_od', 4, 1)->nullable();
            $table->decimal('iop_os', 4, 1)->nullable();
            $table->enum('iop_method', ['goldmann', 'air_puff', 'icare', 'tonopen'])->nullable();

            $table->string('pupils_note', 255)->nullable();
            $table->json('slit_lamp')->nullable();
            $table->json('fundus')->nullable();

            // Optic disc cup/disc ratio (glaucoma)
            $table->decimal('cup_disc_od', 3, 2)->nullable();      // 0.00..1.00
            $table->decimal('cup_disc_os', 3, 2)->nullable();

            // Diabetic retinopathy grading (UK NSC: R0-R3 retinopathy, M0-M1 maculopathy)
            $table->enum('dr_grade_od', ['R0', 'R1', 'R2', 'R3'])->nullable();
            $table->enum('dr_grade_os', ['R0', 'R1', 'R2', 'R3'])->nullable();
            $table->enum('maculopathy_od', ['M0', 'M1'])->nullable();
            $table->enum('maculopathy_os', ['M0', 'M1'])->nullable();

            $table->text('notes')->nullable();
            $table->unsignedInteger('examined_by')->nullable();    // users.id
            $table->timestamp('examined_at')->nullable();
            $table->timestamps();

            $table->index(['business_id', 'patient_id'], 'opth_exam_biz_patient_idx');
            $table->index('appointment_id', 'opth_exam_appt_idx');
            $table->foreign('patient_id')->references('id')->on('clc_patients')->onDelete('cascade');
        });
    }

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