<?php

namespace App\Models;

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

class NutritionDay extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = ['nutrition_plan_id', 'label', 'sort_order'];

    public function plan()
    {
        return $this->belongsTo(NutritionPlan::class, 'nutrition_plan_id');
    }

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

    /**
     * مجموع کالری روزانه — همیشه محاسبه‌شده، هرگز ذخیره‌شده (بند ۲۳؛ دلیل در docs).
     */
    public function totalCalories(): int
    {
        return $this->meals->sum(fn (NutritionMeal $meal) => $meal->totalCalories());
    }
}
