<?php

namespace Modules\NX600Lab\Entities;

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

class NxPatient extends Model
{
    protected $table = 'nx_patients';

    protected $fillable = [
        'business_id', 'patient_id', 'patient_name', 'sex', 'age', 'species',
        'contact_id', 'vc_pet_id',
    ];

    protected $casts = [
        'sex'     => 'integer',
        'age'     => 'integer',
        'species' => 'integer',
    ];

    public function testSessions(): HasMany
    {
        return $this->hasMany(NxTestSession::class, 'patient_id', 'patient_id');
    }

    public function getSexLabelAttribute(): string
    {
        return match ($this->sex) {
            0 => 'Male',
            1 => 'Female',
            default => 'Unknown',
        };
    }

    public function getSpeciesNameAttribute(): string
    {
        $map = NxModuleSetting::speciesMap($this->business_id);
        return $map[$this->species] ?? "Species #{$this->species}";
    }

    public function getAgeLabelAttribute(): string
    {
        return $this->age === 999 ? 'N/A' : (string) $this->age;
    }
}
