<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Model\Timeline;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Validator;
use Helper;


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

    public function getAll()
    {
        $query = Timeline::where('is_approved', 'approved')->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 getByUser($user_id)
    {
        $query = Timeline::where('user_id', Helper::hash($user_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 getSingle($data_id)
    {
        $query = Timeline::where('id', Helper::hash($data_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 postStore(Request $request)
    {
        $post = $request->all();

        $rules = [
            'gambar' => 'required',
            'user_id' => 'required'
        ];

        $validator = Validator::make($post, $rules);

        if ($validator->passes()) {

            $query                  = new Timeline();
            $query->is_approved     = 'pending';
            $query->caption         = $request->caption;
            $query->timeline_photo  = Helper::base64_to_image($request->gambar, 'timeline');
            $query->user_id         = Helper::hash($request->user_id, 'decode');
            $query->save();

            $query->hash_id = Helper::hash($query->id, 'encode');
            $query->user_id = Helper::hash($query->user_id, 'encode');


            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 postUpdate(Request $request)
    {        
        $post = $request->all();

        $rules = [
            'id' => 'required',
            'caption' => 'required'
        ];

        $validator = Validator::make($post, $rules);

        if(!$validator->fails()){
            $timeline = Timeline::where('id', Helper::hash($request->id, 'decode'))->firstOrFail();
            
            $timeline->caption               = $request->caption;
            $timeline->save();

            $timeline->hash_id = Helper::hash($timeline->id, 'encode');
            $timeline->user_id = Helper::hash($timeline->user_id, 'encode');

            $result = $timeline;
            $res_status = true;
            $msg = 'Berhasil Update Data';
            $status_msg = $msg;

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

        }else {

            $res_status = false;
            $msg = 'Gagal Update Data';
            $status_msg = $validator->errors();

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

    public function delete(Request $request)
    {
        $post = $request->all();

        $rules = [
            'id' => 'required'
        ];

        $validator = Validator::make($post, $rules);

        if(!$validator->fails()){
            $query = Timeline::find(Helper::hash($request->id, 'decode'))->delete();

            if($query){
                $result = $query;
                $res_status = true;
                $msg = 'Data berhasil dihapus';
                $status_msg = $msg;

                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
            }else{
                $result = $query;
                $res_status = false;
                $msg = 'Data gagal dihapus';
                $status_msg = $msg;

                return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
            }
        }else{
            $res_status = false;
            $msg = 'Data tidak ditemukan';
            $status_msg = $validator->errors();

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

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


}