<?php

namespace App\Exceptions;

use Carbon\Carbon;
use Dotenv\Exception\ValidationException;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Facades\Auth;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];


    public function report(Exception $exception)
    {
        $this->sendErrorToControlPanel($exception);
        parent::report($exception);
    }


    public function render($request, Exception $exception)
    {
        if($exception instanceof TokenMismatchException)
        {
            return  redirect(url()->previous())->withErrors('Token Expired Kindly Refresh  Page and Try Again  !!!!');
        }
        return parent::render($request, $exception);
    }

    /**
     * Sends errors to control panel api
     * @param Exception $exception
     */
    private function sendErrorToControlPanel(Exception $exception)
    {
        //error message  to submit
        $requestParam = [
            'message' => '"' . $exception->getMessage() . '" in file "' . $exception->getFile() . '" line ' . $exception->getLine(),
            'current_url' => url()->current(),
            'session' => session()->all()
        ];


        // if we are in local development, do not send error
        // omit this check if testing, ensure support is aware this is just a test.
        if (app()->environment('local')) return;

        //initialize curl
        $curl = curl_init();
        //prepare data to be sent
        $data = [
            'application_id' => 2, // using the api endpoint in the docs, ensure you use the id for this app. if id does not exist, ensure it is created
            'description' => json_encode($requestParam),
            'secret' => 'VZKXKKD8GNJNABSOSRELFTUCBVBUGIXGEHZVJA5NRAFZ7BHGRDYQY5HAUYW9'
        ];

        //attach the client name and email if authenticated.
        if (Auth::user() != null) {
            //use the field in the table for the application ie name, fullname, username etc
            $data['client_name'] = auth()->user()->full_name;
            //if email is available, add this line
            $data['client_email'] = auth()->user()->email;
        }

        //set curl parameters
        curl_setopt_array($curl,
            [
                CURLOPT_URL => "https://controlpanel-api.npontu.com/api/v1/tickets/alert/application",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => $data
            ]
        );

        //response from the api
        $response = curl_exec($curl);
        curl_close($curl);
    }

}
