<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;

class MainController extends BaseController{
    /**
     * response that encodes the data
     *
     * @return base64_encoded response
     */
    public function apiResponse($json,$status=200,$error="",$encode=true){
        if($error != "")
            $json['error'] = $error;
        $response = $json;
        // if($status == 200){
        //     if($encode)
        //         $response = base64_encode(json_encode($json));
        // }
        return response()->json($response,$status);
    }
    /*
     * TRANS DB
     * 
     * 
     */
    public function transDB($callback){
        $connection = DB::reconnect();
        $connection->transaction($callback);
    }
    /*
     * LOG ERROR
     * 
     * 
     */
    public function logError($e,string $trans_type, int $client_id,string $user,string $occured_on=null, int $trans_id=null){
      $log = null;

      // $type = "";
      // if($e instanceof NotFoundHttpException){
      //     $type = "Route Not Found";
      // } else if($e instanceof QueryException) {
      //     $type = "Query Error";
      // } else {
      //     $type = "Server Error";
      // }

      // $error_msg = $e->getMessage();
      // $error_line = $e->getLine();
      // $error_file = $e->getFile();

      // $code = "FM".Carbon::now()->timestamp;
      // if(is_array($error_line)){
      //     $error_line = "Error is an array";
      // }
      // if(is_array($error_file)){
      //     $error_file = "Error is an array";
      // }
      // if(is_array($error_msg)){
      //     $error_msg = "Error is an array";
      // }
      // $file = $type." Line: ".$error_line." - ".$error_file;

      // $log = new ErrorLogsModel();
      // $log->client_id = $client_id;
      // $log->trans_type = $trans_type;
      // $log->created_by = $user;
      // $log->occured_on = $occured_on;
      // $log->trans_id = $trans_id;
      // $log->code = $code;
      // $log->type = $type;
      // $log->file = $file;
      // $log->description = $error_msg;
      // $log->save();
      
      return $log;
    }
}
