<?php

namespace Modules\Ingredient\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Ingredient\Models\IngredientCategory;
use Modules\Ingredient\Repositories\IngredientCategoryRepository;

class IngredientCategoryController extends Controller
{
    private $ingredientCategoryRepository;
    /**
     * Display a listing of the resource.
     */
    public function __construct(
        IngredientCategoryRepository $ingredientCategoryRepository) {
        $this->ingredientCategoryRepository = $ingredientCategoryRepository;
    }

    public function index()
    {
        $data['title'] = 'Categories List';
        $data['categories'] = $this->ingredientCategoryRepository->findAll();
        return view('ingredient::ingredientCategory.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'Create IngredientCategory';
        $data['status'] = IngredientCategory::STATUS;

        return view('ingredient::ingredientCategory.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        $request->request->add(['slug' => slugify($request->title)]);
        $inputData = $request->all();
        $this->ingredientCategoryRepository->create($inputData);
        toastr()->success('IngredientCategory Created Succesfully');

        return redirect()->route('ingredientCategory.index');
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $data['title'] = 'Show IngredientCategory';
        $data['status'] = IngredientCategory::STATUS;
        $data['category'] = $this->ingredientCategoryRepository->getIngredientCategoryById($id);

        return view('ingredient::ingredientCategory.show', $data);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $data['title'] = 'Edit IngredientCategory';
        $data['status'] = IngredientCategory::STATUS;

        $data['category'] = $this->ingredientCategoryRepository->getIngredientCategoryById($id);

        return view('ingredient::ingredientCategory.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        $inputData = $request->except(['_method', '_token']);
        $this->ingredientCategoryRepository->update($id, $inputData);

        return redirect()->route('ingredientCategory.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
          try {
            $IngredientCategoryModel = $this->ingredientCategoryRepository->getIngredientCategoryById($id);
            $IngredientCategoryModel->delete();

            toastr()->success('Ingredient Delete Succesfully');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }

        return response()->json(['status' => true, 'message' => 'IngredientCategory Delete Succesfully']);
    }
}