<?php

namespace Modules\ClinicCore\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * SIFA P3-T6 — a line of a unified clinical order. downstream_type/downstream_id
 * point at the row created in the routed-to flow (a clc_imaging_requests row or a
 * lab_worklist row); result_summary + abnormal_flag capture the outcome.
 */
class ClcOrderItem extends Model
{
    protected $table = 'clc_order_items';
    protected $guarded = ['id'];

    protected $casts = [
        'scheduled_at' => 'datetime',
    ];

    public const LATERALITIES = ['left', 'right', 'bilateral', 'na'];
    public const STATUSES     = ['ordered', 'in_progress', 'resulted', 'cancelled'];
    public const ABNORMAL     = ['normal', 'high', 'low', 'critical_high', 'critical_low'];

    public function order()
    {
        return $this->belongsTo(ClcOrder::class, 'order_id');
    }

    /** Whether this item's result is a critical value (raises a medical alert). */
    public function isCritical(): bool
    {
        return in_array($this->abnormal_flag, ['critical_high', 'critical_low'], true);
    }
}
