<?php

namespace Modules\VetAdmission\Entities;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class VaAdmissionFluid extends Model
{
    protected $table = 'va_admission_fluids';
    protected $guarded = ['id'];

    protected $casts = [
        'started_at'        => 'datetime',
        'estimated_end_at'  => 'datetime',
        'ended_at'          => 'datetime',
        'active'            => 'boolean',
    ];

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

    public function bagChanges()
    {
        return $this->hasMany(VaFluidBagChange::class, 'fluid_id')->orderByDesc('changed_at');
    }

    /** True when the current bag is past its estimated finish time. */
    public function getIsBagOverdueAttribute(): bool
    {
        if (!$this->active || !$this->estimated_end_at) return false;
        return Carbon::now()->gt($this->estimated_end_at);
    }
}
