<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Model\Raharja\Tips;
use App\Model\TipsLog;
use App\Model\PointLog;
use App\Model\Raharja\TipsLikes;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Helper;
use Request;
use Validator;

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

    public function getAll()
    {
        $query = Tips::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 = Tips::where('id', Helper::hash($data_id, 'decode'))->get();

        $query = array_map(function ($item) {
            $item['likes'] = TipsLikes::where('tips_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'],
            'tips_id' => ['required'],
            'point' => ['required'],
        ]);

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

            $total = Request::all()['point'];
            
            $point = new PointLog();
            $point->user_id = Helper::hash($tips_logs->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 ($tips_logs && $point) {
                $res_status = true;
                $msg = 'Mendapatkan Data';
                $status_msg = $msg;
                $result = [
                    'tips_logs' => $tips_logs,
                    '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_tips = Tips::where('id', Helper::hash($data_id, 'decode'))->first();

        if($arr_tips){
            $get_tips_by_user = TipsLikes::where('tips_id', $arr_tips->id)->where('user_id', 123456)->first();

            if($get_tips_by_user){
                $query = $get_tips_by_user->delete();
            }else {
                $query = TipsLikes::create([
                    'tips_id' => $arr_tips->id,
                    'user_id' => 123456,
                ]);
            }

            $result = $query;

            $res_status = true;
            $msg = 'Berasil likes atau unlikes tips';
            $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);
        }
    }

}
