<?php

namespace Modules\MedWard\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * A Medication Administration Record entry (SIFA P6-T2 MAR). Generated as
 * `scheduled` by MarService from active inpatient Rx items, then administered /
 * held by a nurse. witness_user_id records the controlled-med second signer.
 */
class MedwardMarEntry extends Model
{
    protected $table = 'medward_mar_entries';

    protected $guarded = ['id'];

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

    public const STATUSES = ['scheduled', 'given', 'held', 'refused', 'missed'];

    public function admission()
    {
        return $this->belongsTo(MedwardAdmission::class, 'admission_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    /** Bootstrap label class per MAR status. */
    public function statusClass(): string
    {
        return [
            'scheduled' => 'label-default',
            'given'     => 'label-success',
            'held'      => 'label-warning',
            'refused'   => 'label-warning',
            'missed'    => 'label-danger',
        ][$this->status] ?? 'label-default';
    }
}
