<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CreateLeadHistoryAPIRequest;
use App\Http\Requests\Api\UpdateLeadHistoryAPIRequest;
use App\Models\LeadHistory;
use App\Repositories\Admin\LeadHistoryRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Illuminate\Http\Response;

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

class LeadHistoryAPIController extends AppBaseController
{
    /** @var  LeadHistoryRepository */
    private $leadHistoryRepository;

    public function __construct(LeadHistoryRepository $leadHistoryRepo)
    {
        $this->leadHistoryRepository = $leadHistoryRepo;
    }

    /**
     * @param Request $request
     * @return mixed
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     * @return Response
     *
     * @SWG\Get(
     *      path="/lead-histories",
     *      summary="Get a listing of the LeadHistories.",
     *      tags={"LeadHistory"},
     *      description="Get all LeadHistories",
     *      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/LeadHistory")
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request)
    {
        $leadHistories = $this->leadHistoryRepository
            ->pushCriteria(new RequestCriteria($request))
            ->pushCriteria(new LimitOffsetCriteria($request))
            //->pushCriteria(new leadHistoryCriteria($request))
            ->all();

        return $this->sendResponse($leadHistories->toArray(), 'Lead Histories retrieved successfully');
    }

    /**
     * @param CreateLeadHistoryAPIRequest $request
     * @return Response
     *
     * @SWG\Post(
     *      path="/lead-histories",
     *      summary="Store a newly created LeadHistory in storage",
     *      tags={"LeadHistory"},
     *      description="Store LeadHistory",
     *      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="LeadHistory that should be stored",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/LeadHistory")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/LeadHistory"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function store(CreateLeadHistoryAPIRequest $request)
    {
        $leadHistories = $this->leadHistoryRepository->saveRecord($request);

        return $this->sendResponse($leadHistories->toArray(), 'Lead History saved successfully');
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Get(
     *      path="/lead-histories/{id}",
     *      summary="Display the specified LeadHistory",
     *      tags={"LeadHistory"},
     *      description="Get LeadHistory",
     *      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 LeadHistory",
     *          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/LeadHistory"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function show($id)
    {
        /** @var LeadHistory $leadHistory */
        $leadHistory = $this->leadHistoryRepository->findWithoutFail($id);

        if (empty($leadHistory)) {
            return $this->sendErrorWithData(['Lead History not found']);
        }

        return $this->sendResponse($leadHistory->toArray(), 'Lead History retrieved successfully');
    }

    /**
     * @param int $id
     * @param UpdateLeadHistoryAPIRequest $request
     * @return Response
     *
     * @SWG\Put(
     *      path="/lead-histories/{id}",
     *      summary="Update the specified LeadHistory in storage",
     *      tags={"LeadHistory"},
     *      description="Update LeadHistory",
     *      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 LeadHistory",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="LeadHistory that should be updated",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/LeadHistory")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/LeadHistory"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function update($id, UpdateLeadHistoryAPIRequest $request)
    {
        /** @var LeadHistory $leadHistory */
        $leadHistory = $this->leadHistoryRepository->findWithoutFail($id);

        if (empty($leadHistory)) {
            return $this->sendErrorWithData(['Lead History not found']);
        }

        $leadHistory = $this->leadHistoryRepository->updateRecord($request, $leadHistory);

        return $this->sendResponse($leadHistory->toArray(), 'LeadHistory updated successfully');
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/lead-histories/{id}",
     *      summary="Remove the specified LeadHistory from storage",
     *      tags={"LeadHistory"},
     *      description="Delete LeadHistory",
     *      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 LeadHistory",
     *          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 LeadHistory $leadHistory */
        $leadHistory = $this->leadHistoryRepository->findWithoutFail($id);

        if (empty($leadHistory)) {
            return $this->sendErrorWithData(['Lead History not found']);
        }

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

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