<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Model\Raharja\News;
use App\Model\NewsLog;
use App\Model\PointLog;
use App\Model\Raharja\NewsLikes;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Helper;
use Request;
use Validator;

class NewsController extends Controller
{
    public function __construct()
    {
        DB::enableQueryLog();
    }

    public function getAll()
    {
        $query = News::where('date_publish', '>=', Carbon::now()->format('Y-m-d'))
        ->get();
        $query = array_map(function ($item) {
            $value['thumbnail']    = $item['thumbnail'];
            $value['title']        = $item['title'];
            $value['publish_date'] = $item['date_publish'];
            return $value;
        }, $query->toArray());

        if($query){
            $result = $query;

            $res_status = true;
            $msg = 'Mendapatkan Data';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);

        }else {

            $res_status = false;
            $msg = 'Data tidak ditemukan';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
        }
    }
    public function getSingle($data_id)
    {
        $query = News::where('id', Helper::hash($data_id, 'decode'))->get();
        $query = array_map(function ($item) {
            $item['likes'] = NewsLikes::where('news_id', $item['id'])->count();
            return $item;
        }, $query->toArray());

        if($query){
            $result = $query;

            $res_status = true;
            $msg = 'Mendapatkan Data';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);

        }else {

            $res_status = false;
            $msg = 'Data tidak ditemukan';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
        }
    }
    
    public function postLog(Request $request)
    {
        $validator = Validator::make(Request::all(), [
            'user_id' => ['required'],
            'news_id' => ['required'],
            'point' => ['required'],
        ]);

        if(!$validator->fails()){
            $news_log = new NewsLog;
            $news_log->user_id = Helper::hash(Request::all()['user_id'], 'decode');
            $news_log->news_id = Helper::hash(Request::all()['news_id'], 'decode');
            $news_log->point = Request::all()['point'];
            $news_log->save();
            $news_log->user_id = Request::all()['user_id'];
            $news_log->news_id = Request::all()['news_id'];
            $news_log->hash_id = Helper::hash($news_log->id, 'encode');

            $total = Request::all()['point'];
            
            $point = new PointLog();
            $point->user_id = Helper::hash($news_log->user_id, 'decode');
            $point->type_point = 'reading_news';
            $point->point = $total;
            $point->save();
            $point->user_id = Helper::hash($point->user_id, 'encode');
            $point->hash_id = Helper::hash($point->id, 'encode');

            if ($news_log && $point) {
                $res_status = true;
                $msg = 'Mendapatkan Data';
                $status_msg = $msg;
                $result = [
                    'news_log' => $news_log,
                    'point' => $point
                ];
    
                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
            }else{
                $res_status = false;
                $msg = 'Data tidak ditemukan';
                $status_msg = $msg;

                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
            }
        }else {

            $res_status = false;
            $msg = 'Error';
            $status_msg = $validator->errors();

            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
        }
    }

    public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
    {
        $response['response'] = array(
            'status'     => $status,
            'message'    => $msg,
            'status_msg' => $status_msg,
        );
        $response['param'] = !empty($param) ? $param : '';

        $response['results'] = $result;

        return response()->json($response, 200);
    }

    public function postLike($data_id){
        $arr_news = News::where('id', Helper::hash($data_id, 'decode'))->first();

        if($arr_news){
            $get_news_by_user = NewsLikes::where('news_id', $arr_news->id)->where('user_id', 123456)->first();

            if($get_news_by_user){
                $query = $get_news_by_user->delete();
            }else {
                $query = NewsLikes::create([
                    'news_id' => $arr_news->id,
                    'user_id' => 123456,
                ]);
            }

            $result = $query;

            $res_status = true;
            $msg = 'Berasil likes atau unlikes news';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);

        }else {

            $res_status = false;
            $msg = 'Data tidak ditemukan';
            $status_msg = $msg;

            return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
        }
    }

}
