<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class NutritionMeal extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = ['nutrition_day_id', 'name', 'consumption_time', 'sort_order'];

    protected function casts(): array
    {
        return ['consumption_time' => 'datetime:H:i'];
    }

    public function day()
    {
        return $this->belongsTo(NutritionDay::class, 'nutrition_day_id');
    }

    public function items()
    {
        return $this->hasMany(NutritionItem::class)->orderBy('sort_order');
    }

    public function totalCalories(): int
    {
        return (int) $this->items->sum('calories');
    }
}
