<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;

use Cms\Classes\Controller;
use BackendMenu;
use DB;
use Config;
use Illuminate\Http\Request;
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
use Illuminate\Support\Facades\Validator;
use Tps\Hhm\Models\Contents;
use Tps\Hhm\Classes\ContentResource;


class ContentsController extends Controller
{
    protected $Contents;

    protected $helpers;

    public function __construct(Contents $Contents, Helpers $helpers)
    {
        parent::__construct();
        $this->Contents    = $Contents;
        $this->helpers          = $helpers;
    }

    public function index(Request $request){

        $data = $request->all();

        $validator = Validator::make($data, [
            'locale' => 'required|in:ru,en,tm',
            'type' => 'required'
        ]);

        if($validator->fails()) {
            return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
        }

        $contentType = $request->get('type');

        $data = response()->json(
            ContentResource::collection($this->Contents::where("type", $contentType)->get()));
            return $data;

    }




    public static function getAfterFilters() {return [];}
    public static function getBeforeFilters() {return [];}
    public static function getMiddleware() {return [];}
    public function callAction($method, $parameters=false) {
        return call_user_func_array(array($this, $method), $parameters);
    }

}
