<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CreateThreadAPIRequest;
use App\Http\Requests\Api\UpdateThreadAPIRequest;
use App\Models\Thread;
use App\Repositories\Admin\ThreadRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Illuminate\Http\Response;

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

class ThreadAPIController extends AppBaseController
{
    /** @var  ThreadRepository */
    private $threadRepository;

    public function __construct(ThreadRepository $threadRepo)
    {
        $this->threadRepository = $threadRepo;
    }

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

        return $this->sendResponse($threads->toArray(), 'Threads retrieved successfully');
    }

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

        return $this->sendResponse($threads->toArray(), 'Thread saved successfully');
    }

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

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

        return $this->sendResponse($thread->toArray(), 'Thread retrieved successfully');
    }

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

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

        $thread = $this->threadRepository->updateRecord($request, $thread);

        return $this->sendResponse($thread->toArray(), 'Thread updated successfully');
    }

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

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

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

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