<?php

namespace App\Controller;

use App\Entity\Pages;
use App\Repository\PagesRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class PagesController extends AbstractController
{
    #[Route(path: '/pages/{slug}', name: 'pages_display')]
    public function display(PagesRepository $pagesRepository, string $slug): Response
    {
        $page = $pagesRepository->findOneBy(criteria: [
            'slug' => $slug,
        ]);

        if (!$page) {
            $page = new Pages();
            $page->setName(name: 'Not Found');
            $page->setContent(content: '404 - The requested page was not found.');
        }

        return $this->render(view: '@default/pages/index.html.twig', parameters: [
            'page' => $page,
        ]);
    }
}
