<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Security\Voter;

use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Entity\User;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

class AttendanceVoter extends Voter
{
    public const string VIEW = 'view';

    public function __construct(private readonly Security $security)
    {
    }

    protected function supports($attribute, $subject): bool
    {
        if (!\in_array($attribute, ['withdraw', self::VIEW], true)) {
            return false;
        }

        return $subject instanceof Attendance;
    }

    protected function voteOnAttribute($attribute, $subject, TokenInterface $token, ?\Symfony\Component\Security\Core\Authorization\Voter\Vote $vote = null): bool
    {
        $user = $token->getUser();
        if (!$user instanceof User) {
            return false;
        }

        /** @var Attendance $attendance */
        $attendance = $subject;

        return match ($attribute) {
            'withdraw' => $this->canWithdraw($attendance, $user),
            self::VIEW => $this->isOwner($attendance, $user),
            default => throw new \LogicException('This code should not be reached!'),
        };
    }

    private function canWithdraw(Attendance $attendance, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        return $this->isOwner($attendance, $user);
    }

    /**
     * The authenticated member owns the attendance (its participant is bound to
     * their account), or they are an admin. Anonymous access to the confirmation
     * page is granted separately by a signed URL / consent token in the controller.
     */
    private function isOwner(Attendance $attendance, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        $participant = $attendance->getParticipant();
        if (!$participant instanceof \Ferienpass\CoreBundle\Entity\Participant\ParticipantInterface) {
            return false;
        }

        if (!($member = $participant->getUser()) instanceof User) {
            return false;
        }

        return $member->getId() === $user->getId();
    }
}
