<?php


namespace App\Repositories;


use App\Models\User;
use App\UsersRecipes;

class UsersRepository implements UsersRepositoryInterface
{
    public function getAll()
    {
        return User::all();
    }

    public function paginate($chunkSize)
    {
        return User::paginate($chunkSize);
    }

    public function create($data)
    {
        return User::create($data);
    }

    public function delete($id)
    {
        $user = User::find($id);
        return $user->delete();
    }

    public function getById($id)
    {
        $user = User::find($id);
        return $user;
    }

    public function getWhereEquals($field, $actual)
    {
        return User::where($field, $actual);
    }

    public function saveRecipe(int $userID, int $recipeID)
    {
        return UsersRecipes::create(['user_id' => $userID, 'recipe_id' => $recipeID]);
    }

    public function getRecipeHistory(int $userID)
    {
        $userRecipes = UsersRecipes::where('user_id', $userID)->get();
        $recipeHistory = [];

        foreach ($userRecipes as $userRecipe) {
            $recipeHistory[] = array_merge(['date' => $userRecipe->created_at], $userRecipe->recipe->nutrition);
        }

        return $recipeHistory;
    }
}