<?php

namespace Assetdocs\Incidentreporting\Http\Controllers;

use App\Http\Controllers\Controller as BaseController;
use Assetdocs\Incidentreporting\Interfaces\IncidentreportingRepositoryInterface;
use Assetdocs\Incidentreporting\Repositories\Presenter\IncidentreportingItemTransformer;

/**
 * Pubic API controller class.
 */
class IncidentreportingApiController extends BaseController
{
    /**
     * Constructor.
     *
     * @param type \Assetdocs\Incidentreporting\Interfaces\IncidentreportingRepositoryInterface $incidentreporting
     *
     * @return type
     */
    public function __construct(IncidentreportingRepositoryInterface $incidentreporting)
    {
        $this->middleware('api');

        $this->repository = $incidentreporting;
        parent::__construct();
    }

    /**
     * Show incidentreporting's list.
     *
     * @param string $slug
     *
     * @return response
     */
    protected function index()
    {
        $incidentreportings = $this->repository
            ->pushCriteria(new \Assetdocs\Incidentreporting\Repositories\Criteria\IncidentreportingPublicCriteria())
            ->setPresenter('\\Assetdocs\\Incidentreporting\\Repositories\\Presenter\\IncidentreportingListPresenter')
            ->scopeQuery(function($query){
                return $query->orderBy('id','DESC');
            })->paginate();

        $incidentreportings['code'] = 2000;
        return response()->json($incidentreportings)
                ->setStatusCode(200, 'INDEX_SUCCESS');
    }

    /**
     * Show incidentreporting.
     *
     * @param string $slug
     *
     * @return response
     */
    protected function show($slug)
    {
        $incidentreporting = $this->repository
            ->scopeQuery(function($query) use ($slug) {
            return $query->orderBy('id','DESC')
                         ->where('slug', $slug);
        })->first(['*']);

        if (!is_null($incidentreporting)) {
            $incidentreporting         = $this->itemPresenter($module, new IncidentreportingItemTransformer);
            $incidentreporting['code'] = 2001;
            return response()->json($incidentreporting)
                ->setStatusCode(200, 'SHOW_SUCCESS');
        } else {
            return response()->json([])
                ->setStatusCode(400, 'SHOW_ERROR');
        }

    }
}