<?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 RainLab\Blog\Models\Post;

use RainLab\Blog\Classes\PostResource;

class PostsController extends Controller
{
    protected $Post;

    protected $helpers;

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

    public function index(Request $request){

        $data = $request->all();

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

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

        $filter = [
            'page'             => input('page'),
            'sort'             => input('sort')??'published_at desc',
            'perPage'          => input('count'),
            'search'           => trim(input('search')),
            'category'         => input('category'),
            'date'             => input('date'),
        ];

        if(request()->has('featured')){
            $filter['featured'] = true;
        }

        $data = response()->json(
            PostResource::collection($this->Post::with(['categories:id,name'])->listFrontEnd($filter)));
            return $data;
    }

    public function show($id, Request $request)
    {
        $data = $request->all();
        $validator = Validator::make($data, [
            'locale' => 'required|in:ru,en,tm',
        ]);
        if($validator->fails()) {
            return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
        }

        if(!$post = $this->Post::find($id)) {
            return $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => "Resource id = {$id} could not be found"]);
        } else {
            $obj = Db::table('vdomah_blogviews_views')
                ->where('post_id', $post->getKey());

            if ($obj->count() > 0) {
                $row = $obj->first();

                // $views = $row->views + rand(1,10);;
                $views = $row->views + 1;;

                $obj->update(['views' => $views]);
                $post['views'] = $row->views;
            }
            else {

                Db::table('vdomah_blogviews_views')->insert([
                    'post_id' => $post->getKey(),
                    'views'   => 1
                ]);
            }

            return new PostResource($post);
        }
    }


    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);
    }

}
