<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CreateProductionTeamAPIRequest;
use App\Http\Requests\Api\UpdateProductionTeamAPIRequest;
use App\Models\ProductionTeam;
use App\Repositories\Admin\ProductionTeamRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Illuminate\Http\Response;

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

class ProductionTeamAPIController extends AppBaseController
{
    /** @var  ProductionTeamRepository */
    private $productionTeamRepository;

    public function __construct(ProductionTeamRepository $productionTeamRepo)
    {
        $this->productionTeamRepository = $productionTeamRepo;
    }

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

        return $this->sendResponse($productionTeams->toArray(), 'Production Teams retrieved successfully');
    }

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

        return $this->sendResponse($productionTeams->toArray(), 'Production Team saved successfully');
    }

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

        if (empty($productionTeam)) {
            return $this->sendErrorWithData(['Production Team not found']);
        }

        return $this->sendResponse($productionTeam->toArray(), 'Production Team retrieved successfully');
    }

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

        if (empty($productionTeam)) {
            return $this->sendErrorWithData(['Production Team not found']);
        }

        $productionTeam = $this->productionTeamRepository->updateRecord($request, $productionTeam);

        return $this->sendResponse($productionTeam->toArray(), 'ProductionTeam updated successfully');
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/production-teams/{id}",
     *      summary="Remove the specified ProductionTeam from storage",
     *      tags={"ProductionTeam"},
     *      description="Delete ProductionTeam",
     *      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 ProductionTeam",
     *          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 ProductionTeam $productionTeam */
        $productionTeam = $this->productionTeamRepository->findWithoutFail($id);

        if (empty($productionTeam)) {
            return $this->sendErrorWithData(['Production Team not found']);
        }

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

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