<?php

namespace Modules\OphthalmologyClinic\Entities;

use Illuminate\Database\Eloquent\Model;
use Modules\ClinicCore\Entities\ClcPatient;

/**
 * Ophthalmology-specific 1:1 extension of a shared ClinicCore patient.
 */
class OphthPatientProfile extends Model
{
    protected $table = 'ophth_patient_profiles';
    protected $guarded = ['id'];

    protected $casts = [
        'wears_glasses' => 'boolean',
        'target_iop_od' => 'float',
        'target_iop_os' => 'float',
    ];

    public function patient()
    {
        return $this->belongsTo(ClcPatient::class, 'clc_patient_id');
    }

    /** Compact refraction string for an eye, e.g. "-2.00 / -0.75 x 180". */
    public function refraction(string $eye): ?string
    {
        $sph = $this->{"sphere_$eye"};
        $cyl = $this->{"cylinder_$eye"};
        $axis = $this->{"axis_$eye"};
        if ($sph === null && $cyl === null) return null;
        $s = $sph !== null ? sprintf('%+.2f', $sph) : '—';
        if ($cyl !== null) {
            $s .= sprintf(' / %+.2f', $cyl) . ($axis !== null ? " x {$axis}" : '');
        }
        return $s;
    }
}
