<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\CalendarBundle\Menu;

use Chill\CalendarBundle\Repository\InviteRepository;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Knp\Menu\MenuItem;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;

final readonly class UserMenuBuilder implements LocalMenuBuilderInterface
{
    public function __construct(
        private Security $security,
        private TranslatorInterface $translator,
        private InviteRepository $inviteRepository,
    ) {}

    public function buildMenu($menuId, MenuItem $menu, array $parameters): void
    {
        $user = $this->security->getUser();

        if ($user instanceof User) {
            $invitationsPending = $this->inviteRepository->countPendingInvitesByUser($user);

            if ($this->security->isGranted('ROLE_USER')) {
                $menu->addChild('My calendar list', [
                    'route' => 'chill_calendar_calendar_list_my',
                ])
                    ->setExtras([
                        'order' => 8,
                        'icon' => 'tasks',
                    ]);
                $menu->addChild(
                    $this->translator->trans('invite.menu with counter', ['nb' => $invitationsPending]),
                    ['route' => 'chill_calendar_invitations_list_my']
                )
                    ->setExtras([
                        'order' => 9,
                        'icon' => 'tasks',
                        'counter' => 0 < $invitationsPending ? $invitationsPending : null,
                    ]);
            }
        }
    }

    public static function getMenuIds(): array
    {
        return ['user'];
    }
}
