<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateMenusAPIRequest;
use App\Http\Requests\API\UpdateMenusAPIRequest;
use App\Models\Menus;
use App\Repositories\MenusRepository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use App\Http\Resources\MenusResource;

/**
 * Class MenusAPIController
 */
class MenusAPIController extends AppBaseController
{
    /** @var  MenusRepository */
    private $menusRepository;

    public function __construct(MenusRepository $menusRepo)
    {
        $this->menusRepository = $menusRepo;
    }

    /**
     * Display a listing of the Menuses.
     * GET|HEAD /menuses
     */
    public function index(Request $request): JsonResponse
    {
        $menuses = $this->menusRepository->all(
            $request->except(['skip', 'limit']),
            $request->get('skip'),
            $request->get('limit')
        );

        return $this->sendResponse(MenusResource::collection($menuses), 'Menuses retrieved successfully');
    }

    /**
     * Store a newly created Menus in storage.
     * POST /menuses
     */
    public function store(CreateMenusAPIRequest $request): JsonResponse
    {
        $input = $request->all();

        $menus = $this->menusRepository->create($input);

        return $this->sendResponse(new MenusResource($menus), 'Menus saved successfully');
    }

    /**
     * Display the specified Menus.
     * GET|HEAD /menuses/{id}
     */
    public function show($id): JsonResponse
    {
        /** @var Menus $menus */
        $menus = $this->menusRepository->find($id);

        if (empty($menus)) {
            return $this->sendError('Menus not found');
        }

        return $this->sendResponse(new MenusResource($menus), 'Menus retrieved successfully');
    }

    /**
     * Update the specified Menus in storage.
     * PUT/PATCH /menuses/{id}
     */
    public function update($id, UpdateMenusAPIRequest $request): JsonResponse
    {
        $input = $request->all();

        /** @var Menus $menus */
        $menus = $this->menusRepository->find($id);

        if (empty($menus)) {
            return $this->sendError('Menus not found');
        }

        $menus = $this->menusRepository->update($input, $id);

        return $this->sendResponse(new MenusResource($menus), 'Menus updated successfully');
    }

    /**
     * Remove the specified Menus from storage.
     * DELETE /menuses/{id}
     *
     * @throws \Exception
     */
    public function destroy($id): JsonResponse
    {
        /** @var Menus $menus */
        $menus = $this->menusRepository->find($id);

        if (empty($menus)) {
            return $this->sendError('Menus not found');
        }

        $menus->delete();

        return $this->sendSuccess('Menus deleted successfully');
    }
}
