<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Model\Trivia;
use App\Model\Points;
use App\Model\TriviaAnswer;
use App\Model\PointLog;
use App\User;
use Illuminate\Support\Facades\Auth;
use Helper;
use Request;
use Validator;

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

    public function getAll()
    {
        $query = Trivia::get();
        $query = array_map(function ($item) {
            $item['answer'] = TriviaAnswer::where('trivia_id', $item['id'])->get();
            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 getSingle($data_id)
    {
        $query = Trivia::where('id', Helper::hash($data_id, 'decode'))->get();
        $query = array_map(function ($item) {
            $item['answer'] = TriviaAnswer::where('trivia_id', $item['id'])->get();
            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 postSubmitAnswer()
    {
        $validator = Validator::make(Request::all(), [
            'user_id' => ['required'],
            'question_id' => ['required'],
            'answer_id' => ['required'],
        ]);

        if(!$validator->fails()){
            $trivia_answer = TriviaAnswer::find(Helper::hash(Request::all()['answer_id'], 'decode'));
            if (($trivia_answer->trivia_id == Helper::hash(Request::all()['question_id'], 'decode')) && ($trivia_answer->is_right == 't')) {
                // Start Perhitungan
                $pointLog = new PointLog();
                $pointLog->user_id = Helper::hash(Request::all()['user_id'], 'decode');
                $pointLog->type_point = 'trivia';
                $pointLog->point = Points::where('type', 'trivia')->pluck('value')->first();
                $pointLog->save();
                // End Perhitungan
                
                $pointLog->user_id = Helper::hash($pointLog->user_id, 'encode');
                $pointLog->answer_id = Helper::hash($pointLog->answer_id, 'encode');
                $pointLog->question_id = Helper::hash($pointLog->question_id, 'encode');
                $pointLog->hash_id = Helper::hash($pointLog->id, 'encode');

                $result = $pointLog;
                $res_status = true;
                $msg = 'Jawaban Benar';
                $status_msg = $msg;
    
                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
            }else{
                $res_status = false;
                $msg = 'Jawaban Salah';
                $status_msg = $msg;

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

            $res_status = false;
            $msg = 'Jawaban Salah';
            $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);
    }

}