<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Database\QueryException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * The list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            //
        });

        $this->renderable(function (MethodNotAllowedHttpException $e, $request){
            $respuesta = json_encode([
                "error" => true,
                "msg" => "ERR_GLB_USU000: Método no soportado.",
                "response" => []
            ]);
    
            return response($respuesta, 405)->header('Content-Type', 'application/json');
        });

        $this->renderable(function (NotFoundHttpException $e, $request){
            $respuesta = json_encode([
                "error" => true,
                "msg" => "ERR_GLB_USU001: La ruta solicitada no existe: " . $request->fullUrl(),
                "response" => []
            ]);
    
            return response($respuesta, 404)->header('Content-Type', 'application/json');
        });

        $this->renderable(function (QueryException $e, $request){
            $respuesta = json_encode([
                "error" => true,
                //"msg" => "ERR_GLB_USU002: No se pudo establecer una conexión con la base de datos. Código de error: " . $e->getCode(),
                "msg" => "ERR_GLB_USU002: No se pudo establecer una conexión con la base de datos. Código de error: " . $e->getMessage(),
                "response" => []
            ]);
    
            return response($respuesta, 500)->header('Content-Type', 'application/json');
        });
    }
}
