<?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\ActivityBundle\Menu;

use Chill\ActivityBundle\Repository\ActivityACLAwareRepositoryInterface;
use Chill\ActivityBundle\Security\Authorization\ActivityVoter;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Chill\PersonBundle\Entity\Person;
use Knp\Menu\MenuItem;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * @implements LocalMenuBuilderInterface<array{person: Person}>
 */
final readonly class PersonMenuBuilder implements LocalMenuBuilderInterface
{
    public function __construct(
        private readonly ActivityACLAwareRepositoryInterface $activityACLAwareRepository,
        private AuthorizationCheckerInterface $authorizationChecker,
        private TranslatorInterface $translator,
    ) {}

    public function buildMenu($menuId, MenuItem $menu, array $parameters)
    {
        /** @var Person $person */
        $person = $parameters['person'];


        $count = $this->activityACLAwareRepository->countByPerson($person, ActivityVoter::SEE);

        if ($this->authorizationChecker->isGranted(ActivityVoter::SEE, $person)) {
            $menu->addChild(
                $this->translator->trans('Activities'),
                [
                    'route' => 'chill_activity_activity_list',
                    'routeParameters' => ['person_id' => $person->getId()],
                ]
            )
                ->setExtras(['order' => 201, 'counter' => $count > 0 ? $count : null]);
        }
    }

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