<?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\Cart\Repository\WithdrawalRequestRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/retractations')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_ORDER')"))]
final class WithdrawalController extends AbstractController
{
    public function __construct(
        private readonly WithdrawalRequestRepository $withdrawalRequestRepository,
    ) {}

    #[Route('', name: 'admin_withdrawal', defaults: ['_locale' => 'fr'], methods: ['GET'])]
    public function index(Request $request): Response
    {
        $search = mb_trim($request->query->getString('search'));
        $status = $request->query->getString('status', 'all');
        if (!\in_array($status, ['all', 'pending', 'processed'], true)) {
            $status = 'all';
        }

        return $this->render('@CpCreationVitiCore/admin/withdrawal/index.html.twig', [
            'items' => $this->withdrawalRequestRepository->findForAdmin(
                $search,
                'all' !== $status ? $status : null,
            ),
            'search' => $search,
            'status' => $status,
        ]);
    }
}
