<?php

namespace Sahbaj\Sahbaj\Http\Controllers;

use App\Http\Controllers\PublicApiController as PublicController;
use Sahbaj\Sahbaj\Interfaces\SahbajRepositoryInterface;
use Sahbaj\Sahbaj\Repositories\Presenter\SahbajItemTransformer;

/**
 * Pubic API controller class.
 */
class SahbajPublicApiController extends PublicController
{
    /**
     * Constructor.
     *
     * @param type \Sahbaj\Sahbaj\Interfaces\SahbajRepositoryInterface $sahbaj
     *
     * @return type
     */
    public function __construct(SahbajRepositoryInterface $sahbaj)
    {
        $this->repository = $sahbaj;
        parent::__construct();
    }

    /**
     * Show sahbaj's list.
     *
     * @param string $slug
     *
     * @return response
     */
    protected function index()
    {
        $sahbajs = $this->repository
            ->setPresenter('\\Sahbaj\\Sahbaj\\Repositories\\Presenter\\SahbajListPresenter')
            ->scopeQuery(function($query){
                return $query->orderBy('id','DESC');
            })->paginate();

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

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

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

    }
}