<?php

namespace App\Exceptions;

use Sentry\State\Scope;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Throwable;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Validation\ValidationException;

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',
    ];

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json([
                'status' => false,
                'error' => 401,
                'message' => trans('app.errors.401')
            ], 401);
        }

        return redirect()->guest('login');
    }

    /**
     * @param Throwable $exception
     * @throws \Exception
     */
    public function report(Throwable $exception)
    {
        //        if (app()->bound('sentry') && $this->shouldReport($exception)) {
        //            $user = request()->user();
        //
        //            if (!empty($user)) {
        //                //$token = Token::where('token', $token)->first();
        //                app('sentry')->configureScope(function (Scope $scope) use ($user): void {
        //                    $scope->setUser([
        //                        'id' => $user->id,
        //                        'phone' => $user->phone,
        //                    ]);
        //                });
        //            }
        //
        //            app('sentry')->captureException($exception);
        //        }

        if (app()->bound('sentry') && $this->shouldReport($exception)) {
            app('sentry')->captureException($exception);
        }

        parent::report($exception);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param Throwable $exception
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * @throws Throwable
     */
    public function render($request, Throwable $exception)
    {
        // Check if the request expects JSON
        if ($request->expectsJson()) {
            // Handle 404 Not Found exception
            if ($exception instanceof NotFoundHttpException) {
                return response()->json([
                    'error' => 'Resource not found'
                ], 404);
            }

            // Handle validation exceptions
            if ($exception instanceof ValidationException) {
                return response()->json([
                    'error' => 'Validation error',
                    'details' => $exception->errors(),
                ], 422);
            }

            // Handle generic exceptions
            return response()->json([
                'error' => $exception->getMessage(),
            ], $this->isHttpException($exception) ? $exception->getStatusCode() : 500);
        }

        // For non-API requests, use the default render method
        return parent::render($request, $exception);
    }
}
