<?php

namespace Modules\LabCore\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class LabTestResult extends Model
{
    protected $table = 'lab_test_results';

    protected $fillable = [
        'test_session_id', 'analyte_code', 'analyte_name', 'base_name',
        'sample_suffix', 'equality', 'value', 'unit', 'dilution_factor',
        'ref_lower', 'ref_upper', 'raw_flag', 'flags', 'is_abnormal',
    ];

    protected $casts = [
        'flags'           => 'array',
        'is_abnormal'     => 'boolean',
        'dilution_factor' => 'integer',
    ];

    public function session(): BelongsTo
    {
        return $this->belongsTo(LabTestSession::class, 'test_session_id');
    }

    /**
     * Best label for display: canonical name if mapped, else raw code.
     */
    public function getLabelAttribute(): string
    {
        return $this->analyte_name ?: $this->analyte_code;
    }
}
