<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Model\Travel;
use App\Model\TravelLog;
use App\Model\PointLog;
use App\Model\Profile;
use App\Model\Raharja\EventOrganisasiMembers;
use App\Model\Raharja\EventOrganisasi;
use Illuminate\Support\Facades\Auth;
use Hash;
use Helper;
use Validator;
use Request;
use Carbon\Carbon;

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

    public function getTravel()
    {
        $query = Travel::get();
        $query = array_map(function ($item) {
            $item['log'] = TravelLog::where('travel_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 getSingleTravel($travel_id)
    {
        $query = Travel::where('travel_id', Helper::hash($travel_id, 'decode'))->get();
        $query = array_map(function ($item) {
            $item['log'] = TravelLog::where('travel_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 getLog($travel_id)
    {
        $query = TravelLog::where('travel_id', Helper::hash($travel_id, 'decode'))->get();

        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 postRequest(Request $request)
    {
        $validator = Validator::make(Request::all(), [
            'user_id' => ['required'],
        ]);
        // dd(Helper::hash(Request::all()['user_id'], 'decode'));

        if(!$validator->fails()){
            $travel = new Travel();
            $travel->user_id = Helper::hash(Request::all()['user_id'], 'decode');
            $travel->save();
            // dd(Helper::hash($travel->id, 'encode'));

            if ($travel) {
                $res_status = true;
                $msg = 'Mendapatkan Data';
                $status_msg = $msg;
                $travel->hash_id = Helper::hash($travel->id, 'encode');
                $travel->user_id = Helper::hash($travel->user_id, 'encode');
                $result = $travel;
    
                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 postLog(Request $request)
    {
        $validator = Validator::make(Request::all(), [
            'travel_id' => ['required'],
            'check_vehicle' => ['required'],
            'take_call' => ['required'],
            'speeding' => ['required'],
            'sudden_brake' => ['required'],
            'bonus_poin' => ['required'],
            'long_distance' => ['required'],
            'time_travel' => ['required'],
        ]);

        if(!$validator->fails()){
            $list_type = ['check_vehicle','take_call','speeding','sudden_brake','bonus_poin'];
            $total = 0;
            foreach ($list_type as $key => $value) {
                $travel_log[$key] = new TravelLog;
                $travel_log[$key]->travel_id = Helper::hash(Request::all()['travel_id'], 'decode');
                $travel_log[$key]->type_point = $value;
                $travel_log[$key]->point = Request::all()[$value];
                $travel_log[$key]->save();
                $travel_log[$key]->travel_id = Request::all()['travel_id'];
                $travel_log[$key]->hash_id = Helper::hash($travel_log[$key]->id, 'encode');

                $total += Request::all()[$value];
            }
            $user = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'))->user_id;
            $is_org = Profile::where('user_id', $user)->pluck('is_have_organization')->first();
            if($is_org == 'yes'){
                $org_id = Profile::where('user_id', $user)->pluck('organization_id')->first();
                $evn_id = EventOrganisasiMembers::where('kelompok_driver_id', $org_id)->pluck('event_organisasi_id')->last(); 
                $evn_check = EventOrganisasi::where('id', $evn_id)->first();
                
                if($evn_check['start_date'] <= Carbon::now()->format('Y-m-d') && $evn_check['end_date'] >= Carbon::now()->format('Y-m-d')){
                    $event_id = 1;
                }else{
                    $event_id = 0;
                }
            }else{
                $event_id = 0;
            }
            $point = new PointLog();
            $point->user_id = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'))->user_id;
            $point->type_point = 'safety_riding';
            $point->point = $total;
            $point->event_id = $event_id;
            $point->save();
            $point->user_id = Helper::hash($point->user_id, 'encode');
            $point->hash_id = Helper::hash($point->id, 'encode');

            $travel = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'));
            $travel->long_distance = Request::all()['long_distance'];
            $travel->time_travel = Request::all()['time_travel'];
            $travel->save();

            if ($travel_log && $point && $travel) {
                $res_status = true;
                $msg = 'Mendapatkan Data';
                $status_msg = $msg;
                $result = [
                    'travel_log' => $travel_log,
                    'point' => $point,
                    'travel' => $travel,
                ];
    
                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);
    }

}
