<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Model\Raharja\Message;
use Illuminate\Support\Facades\DB;
use App\Model\Raharja\UserReceive;
use Illuminate\Support\Facades\Auth;

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

    public function getAll()
    {
        $query  = UserReceive::get();
        $arr_id = collect($query)->pluck('id')->toArray();
        $query = array_map(function ($item)use ($arr_id) {
            $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
            $item['title']    = $arr_message->where('id', $item['message_id'])->first()['title'];
            $item['content']  = $arr_message->where('id', $item['message_id'])->first()['content'];
            $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
            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 = UserReceive::where('message_id', Helper::hash($data_id, 'decode'))->get();
        $arr_id = collect($query)->pluck('message_id')->toArray();
        $query = array_map(function ($item)use ($arr_id) {
            $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
            $item['title']    = $arr_message->where('id', $item['message_id'])->first()['title'];
            $item['content']  = $arr_message->where('id', $item['message_id'])->first()['content'];
            $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
            $item['is_check'] = $item['is_check'];
            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 userViewMessage($message_id, $user_id)
    {
        $query = UserReceive::where('message_id', $message_id)
        ->where('user_id', $user_id)
        ->get();
        $update_status = $query->first()->update(['is_check' => 1]);

        $arr_id = collect($query)->pluck('message_id')->toArray();
        $query = array_map(function ($item)use ($arr_id) {
            $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
            $item['title']    = $arr_message->where('id', $item['message_id'])->first()['title'];
            $item['content']  = $arr_message->where('id', $item['message_id'])->first()['content'];
            $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
            $item['is_check'] = $item['is_check'];
            return $item;
        }, $query->toArray());


        if($query && $update_status){
            $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 countUserMessage($data_id)
    {
        $query = UserReceive::where('user_id', $data_id)
        ->where('is_check', 0)
        ->count();

        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 = Message::whereHas('userReceive', function($item) use ($user_id) {
            $item->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 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);
    }

}
