<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 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\Admin;

use CpCreation\VitiCore\Marketing\Model\ProductReview;
use CpCreation\VitiCore\Marketing\Repository\AbandonedCartConfigurationRepository;
use CpCreation\VitiCore\Marketing\Repository\AbandonedCartReminderRepository;
use CpCreation\VitiCore\Marketing\Repository\ProductReviewRepository;
use CpCreation\VitiCore\Marketing\Repository\ProductReviewReportRepository;
use CpCreation\VitiCore\Marketing\Repository\ReviewInvitationRepository;
use CpCreation\VitiCore\Marketing\Repository\ReviewRequestConfigurationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/marketing')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_MARKETING')"))]
final class MarketingController extends AbstractController
{
    public function __construct(
        private readonly ReviewRequestConfigurationRepository $configurationRepository,
        private readonly ReviewInvitationRepository $invitationRepository,
        private readonly ProductReviewRepository $reviewRepository,
        private readonly ProductReviewReportRepository $reportRepository,
        private readonly AbandonedCartConfigurationRepository $abandonedCartConfigurationRepository,
        private readonly AbandonedCartReminderRepository $abandonedCartReminderRepository,
    ) {}

    #[Route('', name: 'admin_marketing')]
    public function index(): Response
    {
        return $this->render('@CpCreationVitiCore/admin/marketing/index.html.twig', [
            'configuration' => $this->configurationRepository->findOne(),
            'invitation_count' => $this->invitationRepository->count([]),
            'pending_review_count' => $this->reviewRepository->count([
                'status' => ProductReview::STATUS_PENDING,
            ]),
            'open_review_report_count' => $this->reportRepository->countOpen(),
            'abandoned_cart_configuration' => $this->abandonedCartConfigurationRepository->findOne(),
            'abandoned_cart_reminder_count' => $this->abandonedCartReminderRepository->count([]),
        ]);
    }
}
