<?php

namespace App\Exceptions;

use App\Libs\Facades\Localization;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Libs\Facades\RequestContext;
use App\Models\CodeEmbed;
use App\Models\WebsiteObject;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Validation\ValidationException::class,
        \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class
    ];

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

    /**
     * Report or log an exception.
     *
     * @param  \Throwable  $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception)
    {
        if ($exception instanceof HttpException && !$request->expectsJson()) {
            $isMissingTranslation = RequestContext::get('is_missing_translation');
            $lang = Localization::getActiveLocale();
            $websiteObject = WebsiteObject::where('slug', '404')
                ->where('website_id', 1)
                ->join('website_objects_lang', 'website_objects_lang.website_object_id', '=',
                    'website_objects.website_object_id')
                ->where('website_objects_lang.wo_language_id', $lang)
                ->where('website_objects_lang.wo_website_id', 1)
                ->first();

            $object = $websiteObject->objectable()->withBlocks()->first();

            $embeds = CodeEmbed::full()
                ->where('status', 'published')
                ->where('global', true)
                ->orderBy('position', 'asc')
                ->get();
            $view = 'frontend.templates.page.blocks';

            $metaData = new \stdClass();
            $metaData->no_index = true;
            $metaData->meta_title = $isMissingTranslation ? 'Translation not available' : 'Not Found';
            $metaData->meta_keywords = '';
            $metaData->meta_description = '';

            return response()->view($view, [
                'object' => $object,
                'websiteObject' => $websiteObject,
                'objectEmbeds' => null,
                'embeds' => $embeds,
                'metaData' => $metaData,
                'langUris' => null,
                'stickyCta' => null,
                'errorCode' => $isMissingTranslation ? 200 : $exception->getStatusCode()
            ], $isMissingTranslation ? 200 : $exception->getStatusCode());
        }

        return parent::render($request, $exception);
    }
}
