<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;

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

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

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }
        
        // Any JSON Accept header, XHR, or versioned API path — must never return HTML Whoops pages.
        $path = $request->path();
        $isApiRequest = $request->expectsJson()
            || $request->ajax()
            || $request->is('v2019/*')
            || $request->is('dev2019/*')
            || $request->is('v2026/*')
            || strncmp((string) $path, 'v2026/', 7) === 0;

        // If asked to use Laravel's default renderer, force JSON for API routes
        if (isset($_ENV['APP_LOG_JSON']) && $_ENV['APP_LOG_JSON'] === "yes") {
            $response = parent::render($request, $e);
            if ($isApiRequest && !($response instanceof JsonResponse)) {
                $status = method_exists($e, 'getStatusCode') ? (int)$e->getStatusCode() : 500;
                return response()->json([ "status#" => (string)$status, "message#" => $e->getMessage() ], $status);
            }
            return $response;
        }

        // Custom JSON error format
        if (isset($_ENV['APP_LOG_JSON']) && $_ENV['APP_LOG_JSON'] === "true") {
            $status = method_exists($e, 'getStatusCode') ? (int)$e->getStatusCode() : 500;
            if ($isApiRequest) {
                return response()->json([ "status#" => (string)$status, "message#" => $e->getMessage() ], $status);
            }
            return new Response([ "status#" => (string)$status, "message#" => $e->getMessage() ], $status);
        }

        // Default: keep old behavior for non-API, JSON for API.
        if ($isApiRequest) {
            $status = method_exists($e, 'getStatusCode') ? (int)$e->getStatusCode() : 500;
            return response()->json([ "status#" => (string)$status, "message#" => $e->getMessage() ], $status);
        }

        return new Response([ "status#" => "500" ], 500);

    }
}
