<?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\News\Model\Category;
use CpCreation\VitiCore\News\Repository\CategoryRepository;
use CpCreation\VitiCore\News\Repository\NewsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\ValueResolver;

final class NewsController extends AbstractController
{
    public function __construct(
        private readonly NewsRepository $newsRepository,
        private readonly CategoryRepository $categoryRepository,
    ) {
    }

    public function index(): Response
    {
        $news = $this->newsRepository->findAvailableAndPublish();
        $categoriesNews = $this->categoryRepository->findBy([], ['position' => 'ASC']);

        return $this->render('news.html.twig', [
            'classMenu' => 'black',
            'subMenu' => !empty($categoriesNews),
            'news' => $news,
            'categoriesNews' => $categoriesNews,
        ]);
    }

    public function category(
        #[ValueResolver('translation_resolver')]
        Category $category,
    ): Response {
        $categoriesNews = $this->categoryRepository->findBy([], ['position' => 'ASC']);

        return $this->render('news_category.html.twig', [
            'classMenu' => 'black',
            'subMenu' => true,
            'category' => $category,
            'categoriesNews' => $categoriesNews,
        ]);
    }
}
