<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Site;

use CpCreation\VitiCore\Contact\Repository\ContactRepository;
use CpCreation\VitiCore\Contact\Repository\LabelRepository;
use CpCreation\VitiCore\Gite\Repository\GiteRepository;
use CpCreation\VitiCore\Newsletter\Repository\NewsletterRepository;
use CpCreation\VitiCore\Popup\Repository\PopupRepository;
use Doctrine\ORM\NonUniqueResultException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class HomepageController extends AbstractController
{
    public function __construct(
        private readonly PopupRepository $popupRepository,
        private readonly NewsletterRepository $newsletterRepository,
        private readonly GiteRepository $giteRepository,
        private readonly ContactRepository $contactRepository,
        private readonly LabelRepository $labelRepository,
    ) {
    }

    /**
     * @throws NonUniqueResultException
     */
    public function index(): Response
    {
        $popup = $this->popupRepository->findOneOrNull();
        $newsletter = $this->newsletterRepository->findLastNewsletter();
        $cottages = $this->giteRepository->findAll();

        $contact = $this->contactRepository->findOne();
        $labels = $this->labelRepository->findBy([], ['position' => 'ASC']);

        $subMenu = $newsletter ? true : null;

        return $this->render('homepage.html.twig', [
            'popup' => $popup,
            'newsletter' => $newsletter,
            'subMenu' => $subMenu,
            'cottages' => $cottages,
            'contact' => $contact,
            'labels' => $labels,
        ]);
    }
}
