<?php

namespace App\Http\Controllers\Api\App\DietPlan;

use Illuminate\Http\Request;
use App\Services\DietPlanService;
use App\Http\Controllers\Controller;
use App\Http\Resources\Api\App\Meal\{MealResource};
use App\Models\{ItemDetail, Meal, Product, MealItem, User};
use App\Http\Requests\Api\App\DietPlan\DietPlanRequest;
use App\Http\Resources\Api\App\DietPlan\MealItemResource;

class DietPlanController extends Controller
{

    /**
     * DietPlanService instance.
     *
     * @var DietPlanService
     */
    private $dietPlanService;

    public function __construct(DietPlanService $dietPlanService)
    {
        $this->dietPlanService = $dietPlanService;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(DietPlanRequest $request)
    {
        $userId = $request->user_id;
        $product = Product::where(function ($q) use ($userId) {
            $q->where('status', 'approved')->orWhere('user_id', $userId);
        })->findOrFail($request->product_id);
        $number_of_servings = $request->number_of_servings;

        if($request->unit != 'gram') {
            $number_of_servings = $request->number_of_unit * $product->calories_per_gram * $request->number_of_servings;
        }
        
        foreach ($request->meal_ids as $meal_id) {
            $meal = Meal::where('user_id', $userId)->findOrFail($meal_id);
            
            $diet_plan = $this->dietPlanService->createDietPlan($product, $userId, $number_of_servings, $meal, $request->date, $request->unit, $request->number_of_unit);
            if (!$diet_plan) {
                return response()->json(['status' => 'error', 'data' => null, 'message' => trans('Something went wrong')], 500);
            }
        }

        return response()->json(['status' => 'success', 'data' => null, 'message' => trans('Created successfully')]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $id)
    {
        $meal_items = MealItem::where('meal_id', $id)
            ->where('user_id', auth('api')->id())
            ->when($request->date, function ($q) use ($request) {
                $q->where('date', $request->date);
            })
            ->get();
        return MealItemResource::collection($meal_items)->additional(['status' => 'success', 'message' => '']);
    }

    public function calculateItemMacros(DietPlanRequest $request)
    {
        $userId = auth('api')->id();
        $product = Product::where(function ($q) use ($userId) {
            $q->where('status', 'approved')->orWhere('user_id', $userId);
        })->findOrFail($request->product_id);

        $number_of_servings = $request->number_of_servings;

        if($request->unit != 'gram') {
            $number_of_servings = $request->number_of_unit * $product->calories_per_gram * $request->number_of_servings;
        }

        $calories = $this->dietPlanService->getItemCalories($product, $number_of_servings);
        $fat = $this->dietPlanService->getItemFat($product, $number_of_servings);
        $protein = $this->dietPlanService->getItemProtein($product, $number_of_servings);
        $carbs = $this->dietPlanService->getItemCarbs($product, $number_of_servings);


        return response()->json(['status' => 'success', 'data' => ['calories' => round($calories,2), 'fat' => round($fat,2), 'protein' => round($protein,2), 'carbs' => round($carbs,2)], 'message' => '']);
    }
    
}
