<?php

namespace App\Controller\Admin;

use App\Entity\Tenant\News;
use App\Entity\Tenant\Comment;
use App\Entity\Tenant\Category;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class DashboardController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    ) {}

    #[Route('/', name: 'admin_dashboard')]
    public function index(): Response
    {
        $newsRepo = $this->entityManager->getRepository(News::class);
        $commentRepo = $this->entityManager->getRepository(Comment::class);
        $categoryRepo = $this->entityManager->getRepository(Category::class);

        $stats = [
            'totalNews' => $newsRepo->count([]),
            'publishedNews' => $newsRepo->count(['status' => 'published']),
            'draftNews' => $newsRepo->count(['status' => 'draft']),
            'pendingComments' => $commentRepo->count(['status' => 'pending']),
            'totalCategories' => $categoryRepo->count([]),
        ];

        $recentNews = $newsRepo->findBy([], ['createdAt' => 'DESC'], 10);

        return $this->render('admin/dashboard/index.html.twig', [
            'stats' => $stats,
            'recentNews' => $recentNews,
        ]);
    }
}
