<?php

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

/**
 * Ophthalmology's 1:1 patient extension on the shared ClinicCore patient —
 * the current refraction / acuity / IOP baseline. Per-visit detail is captured
 * by the Ophthalmology exam sheet; this is the at-a-glance eye profile.
 */
class CreateOphthPatientProfilesTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('ophth_patient_profiles')) return;
        if (! Schema::hasTable('clc_patients')) return; // requires ClinicCore

        Schema::create('ophth_patient_profiles', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('clc_patient_id');

            // Visual acuity (free text, e.g. 6/6, 20/20, CF, HM)
            $table->string('va_right', 15)->nullable();
            $table->string('va_left', 15)->nullable();

            // Refraction per eye
            $table->decimal('sphere_right', 5, 2)->nullable();
            $table->decimal('cylinder_right', 5, 2)->nullable();
            $table->unsignedSmallInteger('axis_right')->nullable();   // 0..180
            $table->decimal('sphere_left', 5, 2)->nullable();
            $table->decimal('cylinder_left', 5, 2)->nullable();
            $table->unsignedSmallInteger('axis_left')->nullable();

            // Intraocular pressure (mmHg)
            $table->decimal('iop_right', 4, 1)->nullable();
            $table->decimal('iop_left', 4, 1)->nullable();

            $table->boolean('wears_glasses')->default(false);
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique('clc_patient_id');
            $table->foreign('clc_patient_id')->references('id')->on('clc_patients')->onDelete('cascade');
        });
    }

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