<?php

namespace App\Exceptions;

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

use App\Http\Helpers\PageUtility;

class z_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) {
            //
        });
    }

    /**
     * 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 ($this->isHttpException($exception)) {
            if ($exception->getStatusCode() == 404) {
                $dataArr = PageUtility::data( ['class_id' => 8, 'page_id' => 2] );
                if (!empty($dataArr)) {
                    $dataArr['full_url'] = $request->fullUrl();

                    $meta_title = $dataArr['meta_title'];
                    $dataArr['meta_title'] = $meta_title != '' ? strip_tags($meta_title) : strip_tags($dataArr['title']);

                    $appTitle = Config('app.name');
                    $defDataArr = array("def_img_ttl" => $appTitle, "def_img_alt" => $appTitle);

                    return response()->view('themes.frontend.pages.404', compact('defDataArr', 'dataArr'));
                }
            }
        }
        return parent::render($request, $exception);
    }
}
