<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CreateOldcustomerAPIRequest;
use App\Http\Requests\Api\UpdateOldcustomerAPIRequest;
use App\Models\Oldcustomer;
use App\Repositories\Admin\OldcustomerRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Illuminate\Http\Response;

/**
 * Class OldcustomerController
 * @package App\Http\Controllers\Api
 */

class OldcustomerAPIController extends AppBaseController
{
    /** @var  OldcustomerRepository */
    private $oldcustomerRepository;

    public function __construct(OldcustomerRepository $oldcustomerRepo)
    {
        $this->oldcustomerRepository = $oldcustomerRepo;
    }

    /**
     * @param Request $request
     * @return mixed
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     * @return Response
     *
     * @SWG\Get(
     *      path="/oldcustomers",
     *      summary="Get a listing of the Oldcustomers.",
     *      tags={"Oldcustomer"},
     *      description="Get all Oldcustomers",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="Authorization",
     *          description="User Auth Token{ Bearer ABC123 }",
     *          type="string",
     *          required=true,
     *          default="Bearer ABC123",
     *          in="header"
     *      ),
     *      @SWG\Parameter(
     *          name="orderBy",
     *          description="Pass the property name you want to sort your response. If not found, Returns All Records in DB without sorting.",
     *          type="string",
     *          required=false,
     *          in="query"
     *      ),
     *      @SWG\Parameter(
     *          name="sortedBy",
     *          description="Pass 'asc' or 'desc' to define the sorting method. If not found, 'asc' will be used by default",
     *          type="string",
     *          required=false,
     *          in="query"
     *      ),
     *      @SWG\Parameter(
     *          name="limit",
     *          description="Change the Default Record Count. If not found, Returns All Records in DB.",
     *          type="integer",
     *          required=false,
     *          in="query"
     *      ),
     *     @SWG\Parameter(
     *          name="offset",
     *          description="Change the Default Offset of the Query. If not found, 0 will be used.",
     *          type="integer",
     *          required=false,
     *          in="query"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  @SWG\Items(ref="#/definitions/Oldcustomer")
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request)
    {
        $oldcustomers = $this->oldcustomerRepository
            ->pushCriteria(new RequestCriteria($request))
            ->pushCriteria(new LimitOffsetCriteria($request))
            //->pushCriteria(new oldcustomerCriteria($request))
            ->all();

        return $this->sendResponse($oldcustomers->toArray(), 'Oldcustomers retrieved successfully');
    }

    /**
     * @param CreateOldcustomerAPIRequest $request
     * @return Response
     *
     * @SWG\Post(
     *      path="/oldcustomers",
     *      summary="Store a newly created Oldcustomer in storage",
     *      tags={"Oldcustomer"},
     *      description="Store Oldcustomer",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="Authorization",
     *          description="User Auth Token{ Bearer ABC123 }",
     *          type="string",
     *          required=true,
     *          default="Bearer ABC123",
     *          in="header"
     *      ),
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="Oldcustomer that should be stored",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/Oldcustomer")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/Oldcustomer"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function store(CreateOldcustomerAPIRequest $request)
    {
        $oldcustomers = $this->oldcustomerRepository->saveRecord($request);

        return $this->sendResponse($oldcustomers->toArray(), 'Oldcustomer saved successfully');
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Get(
     *      path="/oldcustomers/{id}",
     *      summary="Display the specified Oldcustomer",
     *      tags={"Oldcustomer"},
     *      description="Get Oldcustomer",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="Authorization",
     *          description="User Auth Token{ Bearer ABC123 }",
     *          type="string",
     *          required=true,
     *          default="Bearer ABC123",
     *          in="header"
     *      ),
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of Oldcustomer",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/Oldcustomer"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function show($id)
    {
        /** @var Oldcustomer $oldcustomer */
        $oldcustomer = $this->oldcustomerRepository->findWithoutFail($id);

        if (empty($oldcustomer)) {
            return $this->sendErrorWithData(['Oldcustomer not found']);
        }

        return $this->sendResponse($oldcustomer->toArray(), 'Oldcustomer retrieved successfully');
    }

    /**
     * @param int $id
     * @param UpdateOldcustomerAPIRequest $request
     * @return Response
     *
     * @SWG\Put(
     *      path="/oldcustomers/{id}",
     *      summary="Update the specified Oldcustomer in storage",
     *      tags={"Oldcustomer"},
     *      description="Update Oldcustomer",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="Authorization",
     *          description="User Auth Token{ Bearer ABC123 }",
     *          type="string",
     *          required=true,
     *          default="Bearer ABC123",
     *          in="header"
     *      ),
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of Oldcustomer",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="Oldcustomer that should be updated",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/Oldcustomer")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/Oldcustomer"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function update($id, UpdateOldcustomerAPIRequest $request)
    {
        /** @var Oldcustomer $oldcustomer */
        $oldcustomer = $this->oldcustomerRepository->findWithoutFail($id);

        if (empty($oldcustomer)) {
            return $this->sendErrorWithData(['Oldcustomer not found']);
        }

        $oldcustomer = $this->oldcustomerRepository->updateRecord($request, $oldcustomer);

        return $this->sendResponse($oldcustomer->toArray(), 'Oldcustomer updated successfully');
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/oldcustomers/{id}",
     *      summary="Remove the specified Oldcustomer from storage",
     *      tags={"Oldcustomer"},
     *      description="Delete Oldcustomer",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="Authorization",
     *          description="User Auth Token{ Bearer ABC123 }",
     *          type="string",
     *          required=true,
     *          default="Bearer ABC123",
     *          in="header"
     *      ),
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of Oldcustomer",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="string"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function destroy($id)
    {
        /** @var Oldcustomer $oldcustomer */
        $oldcustomer = $this->oldcustomerRepository->findWithoutFail($id);

        if (empty($oldcustomer)) {
            return $this->sendErrorWithData(['Oldcustomer not found']);
        }

        $this->oldcustomerRepository->deleteRecord($id);

        return $this->sendResponse($id, 'Oldcustomer deleted successfully');
    }
}
