<?php

namespace App\Exceptions;

use App\Console\Commands\Jobs\MailJobs;
use App\Jobs\SlackLogJob;
use App\Models\Jobs;
use Exception;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    /*
    200,OK, The request was successfully completed.
    201,Created,A new resource was successfully created.,
    400,Bad Request,The request was invalid.,
    401,Unauthorized,The request did not include an authentication token or the authentication token was expired.,
    403,Forbidden,The client did not have permission to access the requested resource.,
    404,Not Found,The requested resource was not found.,
    405,Method Not Allowed,The HTTP method in the request was not supported by the resource. For example, the DELETE method cannot be used with the Agent API.,
    409,Conflict,The request could not be completed due to a conflict. For example,  POST ContentStore Folder API cannot complete if the given file or folder name already exists in the parent location.,
    500,Internal Server Error,The request was not completed due to an internal error on the server side.,
    503,Service Unavailable,The server was unavailable.,
     * */

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param \Exception $exception
     * @return void
     */
    public function report(Exception $exception)
    {


        if ($exception instanceof Exception) {

            if (config("app.env") === 'production') {
                $param['attachments'] =
                    [
                        [
                            "color" => "#ea4335",
                            "title" => ":warning: Exception ",
                            "text" => "<" . config("app.url") . "|" . config("app.name") . ">, " .
                                date("d-m-Y h:i:s") . ' ```' .
                                $exception->getFile() . ', ' .
                                $exception->getLine() . ', ' .
                                $exception->getMessage() . '```'
                        ]
                    ];

                $jobCount = Jobs::count();
                if ($jobCount < 100) {
                    //dispatch(new SlackLogJob($param))->onQueue('slackLog');
                } else {
                    Log::error($param);
                }
            }
        }

        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Exception $exception
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
}
