<?php

namespace Modules\MedBloodBank\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * An issued bag + transfusion / reaction record (SIFA P6-T6). A recorded reaction
 * flags the issue and raises a critical, patient-linked medical alert.
 */
class MbbIssue extends Model
{
    protected $table = 'mbb_issues';

    protected $guarded = ['id'];

    protected $casts = [
        'reaction'                 => 'boolean',
        'issued_at'                => 'datetime',
        'transfusion_started_at'   => 'datetime',
        'transfusion_completed_at' => 'datetime',
    ];

    public const ISSUED_TO          = ['or', 'ward', 'er', 'other'];
    public const REACTION_TYPES     = ['febrile', 'allergic', 'hemolytic', 'taco', 'trali', 'other'];
    public const REACTION_SEVERITIES = ['mild', 'moderate', 'severe', 'life_threatening'];
    public const STATUSES           = ['issued', 'transfusing', 'completed', 'returned'];

    public function crossmatch()
    {
        return $this->belongsTo(MbbCrossmatch::class, 'crossmatch_id');
    }

    public function donation()
    {
        return $this->belongsTo(MbbDonation::class, 'donation_id');
    }

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