<?php

namespace Modules\OphthalmologyClinic\Entities;

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

/**
 * A glasses (spectacle) prescription for a shared ClinicCore patient, behind
 * the standard optical Rx print view.
 */
class OpthGlassesRx extends Model
{
    protected $table = 'opth_glasses_rx';
    protected $guarded = ['id'];

    protected $casts = [
        'prescribed_at' => 'datetime',
        'sphere_od'   => 'float', 'sphere_os'   => 'float',
        'cylinder_od' => 'float', 'cylinder_os' => 'float',
        'add_od'      => 'float', 'add_os'      => 'float',
        'pd_mm'       => 'float',
    ];

    public const LENS_TYPES = [
        'single_vision' => 'Single vision',
        'bifocal'       => 'Bifocal',
        'progressive'   => 'Progressive',
        'photochromic'  => 'Photochromic',
    ];

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

    /** Format a signed dioptre value, e.g. +1.25 / -0.75, or an em dash if null. */
    public static function dpt($v): string
    {
        return $v === null ? '—' : sprintf('%+.2f', (float) $v);
    }

    /** Axis rendered as "x NNN" or blank when there's no cylinder/axis. */
    public static function axis($v): string
    {
        return $v === null ? '' : ('x ' . (int) $v);
    }
}
