<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use App\Models;

class Controller extends BaseController{

    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function log_queryBAK() {
      \DB::listen(function ($sql) {
          \Log::info('showing query', array('sql' => $sql));
          }
        );
    }
    public function log_query() {
      // , $binding, $timing 'bindings' => $binding)
      \DB::listen(function ($sql) {
        \Log::info('Showing query', array('sql' => $sql));
        $encoded_sql = json_encode($sql);
        $this->sendNtfy("Showing Query : " . $encoded_sql); 
      });
    }

    public function sendNtfy($data){
      $url = 'https://ntfy.sh/mMjh6hStlSQiyiUr';
      $url_b = 'https://ntfy.sh/SansaTest';
      $curl = curl_init();
      curl_setopt_array($curl, array(
        CURLOPT_HTTPHEADER => array(
          'Content-Type: application/json'
        ),
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => $data
      ));
      $response = curl_exec($curl);
      return $response;

    }
    public function logUsersActivity($type, $content, $user_id = null){
      if($user_id == null){
        $user_id = session('current_user.id');
      }
      $activity_arr = [
            'type' => $type,
            'content' => $content,
            'user_id' => $user_id,
            'ip_address' => \Request::ip(),
            'device' => request()->header('User-Agent')
        ];
        $retval = Models\UserActivity::create($activity_arr);
        return true;
    }
    public function logUsersActivityTwo($content){
      $clientModel = new Models\Client;
      $currentuser = session('current_user.name');
      $user_model = Models\SystemUser::find(session('current_user.id'));
      activity()->performedOn($clientModel)
      ->causedBy($user_model)
      ->log($currentuser . ' Opened the Client Module at: ' . date('Y-m-d H:i:s'));
        return true;
    }
    public function storeLoggedUser(){
      $user_id = session('current_user.id');
      $logged_arr = [
            'user_id' => $user_id,
            'last_seen_time' => date('Y-m-d H:i:s'),
            'ip_address' => \Request::ip(),
            'device' => request()->header('User-Agent')
        ];
        $retval = Models\LoggedUser::create($logged_arr);
        return true;
    }
    public function deleteLoggedUser(){
      $user_id = session('current_user.id');
      $device = request()->header('User-Agent');
      $logged_user = Models\LoggedUser::where('user_id', $user_id)->where('device', $device)->first();
      if ($logged_user) {
        \DB::table('logged_users')->where('id', $logged_user->id)->delete();
      }
      return true;
    }
}
