<?php

namespace App\Http\Controllers\Coach;

use App\Http\Controllers\Controller;
use App\Http\Requests\Coach\NutritionItemRequest;
use App\Models\NutritionItem;
use App\Models\NutritionMeal;
use App\Services\Coach\NutritionBuilderService;
use Illuminate\Http\RedirectResponse;

class NutritionItemController extends Controller
{
    public function __construct(private readonly NutritionBuilderService $builder)
    {
    }

    public function store(NutritionItemRequest $request, NutritionMeal $meal): RedirectResponse
    {
        $this->authorize('update', $meal->day->plan);

        $this->builder->addItem($meal, $request->validated());

        return back()->with('status', 'قلم غذایی اضافه شد.');
    }

    public function update(NutritionItemRequest $request, NutritionItem $item): RedirectResponse
    {
        $this->authorize('update', $item->meal->day->plan);

        $this->builder->updateItem($item, $request->validated());

        return back()->with('status', 'قلم غذایی به‌روزرسانی شد.');
    }

    public function destroy(NutritionItem $item): RedirectResponse
    {
        $this->authorize('update', $item->meal->day->plan);

        $this->builder->deleteItem($item);

        return back()->with('status', 'قلم غذایی حذف شد.');
    }
}
