<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

use GuzzleHttp\Exception\ServerException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\UnauthorizedException;
use ParseError;
use Symfony\Component\ErrorHandler\Error\FatalError;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Http\Exceptions\ThrottleRequestsException;

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

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

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }
    //
    public function render($request, Throwable $exception)
    {
        // return parent::render($request, $exception);
        $message= null;
        $status= null;
        if ($exception instanceof NotFoundHttpException) {
            $message = 'no data is found';
            $status= 404;
        }
        if ($exception instanceof MethodNotAllowedHttpException) {
            $message = 'no data is found';
            $status= 404;
        }
        if ($exception instanceof ModelNotFoundException) {
            $message = 'no data is found';
            $status= 404;
        }
        if ($exception instanceof UnauthorizedException) {
            $message = 'Not Authorized';
            $status= 403;
        }
        if ($exception instanceof AccessDeniedHttpException) {
            $message = 'Not Authorized';
            $status= 403;
        }
        
        if ($exception instanceof AuthorizationException) {
            $message = 'Not Authorized';
            $status= 403;
        }
        if ($exception instanceof ThrottleRequestsException) {
            $message = 'Too Many Attempts.';
            $status= 429;
        }
        
        if ($exception instanceof ServerException) {
            $message = 'Bad Request';
            // $status= 500;
        }
        if ($exception instanceof ParseError){
            $message= 'Parse Error';
            // $status= 500;
        }
        if ($exception instanceof FatalError){
            $message= 'Fatal Error';
            // $status= 500;
        }
        // dd($status);
        if ($status) {
            if( request()->wantsJson()) {
                return response()->json(['message' => $message], $status);
            } else{
                return response()->view('errors.'.$status, [], $status);
            }
        }
        return parent::render($request, $exception);
    }
}
