<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CreateQaLeadAPIRequest;
use App\Http\Requests\Api\UpdateQaLeadAPIRequest;
use App\Models\QaLead;
use App\Repositories\Admin\QaLeadRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Illuminate\Http\Response;

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

class QaLeadAPIController extends AppBaseController
{
    /** @var  QaLeadRepository */
    private $qaLeadRepository;

    public function __construct(QaLeadRepository $qaLeadRepo)
    {
        $this->qaLeadRepository = $qaLeadRepo;
    }

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

        return $this->sendResponse($qaLeads->toArray(), 'Qa Leads retrieved successfully');
    }

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

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

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

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

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

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

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

        $qaLead = $this->qaLeadRepository->updateRecord($request, $qaLead);

        return $this->sendResponse($qaLead->toArray(), 'QaLead updated successfully');
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/qa-leads/{id}",
     *      summary="Remove the specified QaLead from storage",
     *      tags={"QaLead"},
     *      description="Delete QaLead",
     *      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 QaLead",
     *          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 QaLead $qaLead */
        $qaLead = $this->qaLeadRepository->findWithoutFail($id);

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

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

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