<?php

namespace App\Http\Controllers\Api\Dashboard\Admin\Ingredient;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Dashboard\Admin\Ingredient\IngredientRequest;
use App\Http\Resources\Api\Dashboard\Admin\Ingredient\{IngredientResource};
use App\Models\{Product, User};
use Illuminate\Http\Request;

class IngredientController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $ingredients = Product::where('type', 'ingredient')->when($request->keyword, function ($q) use ($request) {
            $q->whereTranslationLike('title', '%' . $request->keyword . '%')
                ->orWhereTranslationLike('desc', '%' . $request->keyword . '%');
        })->latest()->paginate($request->per_page ?? 25);
        return IngredientResource::collection($ingredients)->additional(['status' => 'success', 'message' => '']);
    }

    public function indexWithoutPagination(Request $request)
    {
        $ingredients = Product::where('type', '!=', 'recipe')->where('status', 'approved')->when($request->keyword, function ($q) use ($request) {
            $q->whereTranslationLike('title', '%' . $request->keyword . '%')
                ->orWhereTranslationLike('desc', '%' . $request->keyword . '%');
        })->when($request->type, function ($q) use ($request) {$q->where('type', $request->type); })->latest()->get();
        return IngredientResource::collection($ingredients)->additional(['status' => 'success', 'message' => '']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(IngredientRequest $request)
    {
        $ingredient = Product::create($request->validated());
        return IngredientResource::make($ingredient)->additional(['status' => 'success', 'message' => trans('Created successfully')]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $ingredient = Product::where('type', 'ingredient')->findOrFail($id);
        return IngredientResource::make($ingredient)->additional(['status' => 'success', 'message' => '']);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(IngredientRequest $request, $id)
    {
        $ingredient = Product::where('type', 'ingredient')->findOrFail($id);
        $ingredient->update($request->validated());

        return IngredientResource::make($ingredient)->additional(['status' => 'success', 'message' => trans('Updated successfully')]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $ingredient = Product::where('type', 'ingredient')->findOrFail($id);
        if ($ingredient->delete()) {
            return response()->json(['status' => 'success', 'data' => null, 'message' => trans('Deleted successfully')]);
        }
        return response()->json(['status' => 'fail', 'data' => null, 'message' => trans('Something went wrong, please try again')], 422);
    }
}
