<?php

namespace Assetdocs\Incident\Http\Controllers;

use App\Http\Controllers\Controller as BaseController;
use Assetdocs\Incident\Interfaces\IncidentRepositoryInterface;

class IncidentController extends BaseController
{
    /**
     * Constructor.
     *
     * @param type \Assetdocs\Incident\Interfaces\IncidentRepositoryInterface $incident
     *
     * @return type
     */
    public function __construct(IncidentRepositoryInterface $incident)
    {
        $this->middleware('web');
        $this->setupTheme(config('theme.themes.public.theme'), config('theme.themes.public.layout'));

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

    /**
     * Show incident's list.
     *
     * @param string $slug
     *
     * @return response
     */
    protected function index()
    {
        $incidents = $this->repository
            ->pushCriteria(new \Assetdocs\Incident\Repositories\Criteria\IncidentPublicCriteria())
            ->scopeQuery(function($query){
                return $query->orderBy('id','DESC');
            })->paginate();

        return $this->theme->of('incident::public.incident.index', compact('incidents'))->render();
    }

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

        return $this->theme->of('incident::public.incident.show', compact('incident'))->render();
    }
}