<?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\DataAccess\HostDataAccess;
use Ferienpass\CoreBundle\Entity\Edition;
use Ferienpass\CoreBundle\Entity\Host;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\User;
use Ferienpass\CoreBundle\Enum\OfferTransition;
use Ferienpass\CoreBundle\Repository\HostRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

class OfferVoter extends Voter
{
    public function __construct(private readonly Security $security, private readonly HostRepository $hosts, private readonly HostDataAccess $hostDataAccess, #[Autowire(param: 'ferienpass_admin.participant_list.hosts_can_add')] private readonly bool $hostsCanAddParticipants = false, #[Autowire(param: 'ferienpass_admin.participant_list.hosts_can_edit')] private readonly bool $hostsCanEditParticipantList = false, #[Autowire(param: 'ferienpass_admin.participant_list.hosts_can_transfer')] private readonly bool $hostsCanTransferParticipants = false)
    {
    }

    protected function supports($attribute, $subject): bool
    {
        $operations = [
            'view',
            'create',
            'edit',
            'copy',
            'delete',
            OfferTransition::FINALIZE,
            OfferTransition::TO_DRAFT,
            OfferTransition::APPROVE,
            OfferTransition::UNAPPROVE,
            OfferTransition::PUBLISH,
            OfferTransition::UNPUBLISH,
            OfferTransition::RELAUNCH,
            OfferTransition::CANCEL,
            OfferTransition::COMPLETE,
            'participants.view',
            'participants.add',
            'participants.transfer',
            'participants.edit',
            'participants.reject',
            'participants.confirm',
        ];

        return $subject instanceof OfferInterface && \in_array($attribute, $operations, true);
    }

    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 OfferInterface $offer */
        $offer = $subject;

        return match ($attribute) {
            'view' => $this->canView($offer, $user),
            'edit' => $this->canEdit($offer, $user),
            'create' => $this->canCreate($offer, $user),
            'copy' => $this->canCopy($offer, $user),
            'delete' => $this->canDelete($offer, $user),
            OfferTransition::CANCEL => $this->canCancel($offer, $user),
            OfferTransition::RELAUNCH => $this->canRelaunch($offer, $user),
            OfferTransition::PUBLISH => $this->canManagePublication($offer, $user),
            OfferTransition::UNPUBLISH => $this->canManagePublication($offer, $user),
            OfferTransition::APPROVE, OfferTransition::UNAPPROVE => $this->canManagePublication($offer, $user),
            OfferTransition::FINALIZE => $this->canFinalize($offer, $user),
            OfferTransition::TO_DRAFT => $this->canManagePublication($offer, $user),
            OfferTransition::COMPLETE => $this->canComplete($offer, $user),
            'participants.view' => $this->canViewParticipants($offer, $user),
            'participants.add' => $this->canAddParticipants($offer, $user),
            'participants.transfer' => $this->canTransferParticipants($offer, $user),
            'participants.edit', 'participants.reject', 'participants.confirm' => $this->canEditParticipants($offer, $user),
            default => throw new \LogicException('This code should not be reached!'),
        };
    }

    private function canView(OfferInterface $offer, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        $userHosts = $this->hosts->findByUser($user);
        $userHostIds = array_map(static fn (Host $host) => $host->getId(), $userHosts);

        return $offer->getHosts()->filter(static fn (Host $host) => \in_array($host->getId(), $userHostIds, true))->count() > 0;
    }

    private function canEdit(OfferInterface $offer, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        if (false === $this->canView($offer, $user)) {
            return false;
        }

        if ($offer->isPublished() || $offer->isReviewed()) {
            return false;
        }

        $edition = $offer->getEdition();

        return $edition->isEditableForHosts() && $this->userIsAllowedHost($user, $edition);
    }

    private function canCreate(OfferInterface $offer, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        $edition = $offer->getEdition();

        // Non-admins are not allowed to create on root leve
        if (!$edition) {
            return false;
        }

        if (false === $this->userIsAllowedHost($user, $offer->getEdition())) {
            return false;
        }

        return !$edition->getActiveTasks('host_editing_stage')->isEmpty();
    }

    private function userIsAllowedHost(User $user, Edition $edition): bool
    {
        if ($edition->getHosts()->isEmpty()) {
            return true;
        }

        $userHosts = $this->hosts->findByUser($user);
        $userHostIds = array_map(static fn (Host $host) => $host->getId(), $userHosts);

        return $edition->getHosts()->filter(static fn (Host $host) => \in_array($host->getId(), $userHostIds, true))->count() > 0;
    }

    private function canCopy(OfferInterface $offer, User $user): bool
    {
        return $this->canCreate($offer, $user) && $this->canView($offer, $user);
    }

    private function canDelete(OfferInterface $offer, User $user): bool
    {
        if (false === $this->canView($offer, $user)) {
            return false;
        }

        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        if (!$offer->getAttendances()->isEmpty()) {
            return false;
        }

        $edition = $offer->getEdition();

        return $edition->isEditableForHosts() && $this->userIsAllowedHost($user, $edition);
    }

    private function canCancel(OfferInterface $offer, User $user): bool
    {
        if (false === $this->canView($offer, $user)) {
            return false;
        }

        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        return $this->hostsCanEditParticipantList;
    }

    private function canRelaunch(OfferInterface $offer, User $user): bool
    {
        return $this->canCancel($offer, $user);
    }

    /**
     * Whether the current user may drive an offer through its publication
     * lifecycle: approve/unapprove, publish/unpublish and reverting to draft.
     * Admins always may; hosts may only when the edition opts in via
     * {@see Edition::hostsCanPublish()} and they can view the offer.
     */
    private function canManagePublication(OfferInterface $offer, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        $edition = $offer->getEdition();
        if (null === $edition || !$edition->hostsCanPublish()) {
            return false;
        }

        return $this->canView($offer, $user);
    }

    private function canFinalize(OfferInterface $offer, User $user): bool
    {
        return null !== $offer->getId() && $this->canView($offer, $user);
    }

    private function canComplete(OfferInterface $offer, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        return $this->canView($offer, $user);
    }

    private function canViewParticipants(OfferInterface $offer, User $user): bool
    {
        $edition = $offer->getEdition();
        if (null === $edition) {
            return false;
        }

        if (!$this->security->isGranted('ROLE_ADMIN')) {
            if (!$edition->isParticipantListReleased()) {
                return false;
            }

            // Even when no "publish_lists" task gates the edition, a host must
            // lose access to participant data once the retention window after
            // the offer's end has elapsed.
            if (!$this->hostDataAccess->hasAccess($offer)) {
                return false;
            }
        }

        return $this->canView($offer, $user);
    }

    private function canAddParticipants(OfferInterface $offer, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        if (false === $this->canView($offer, $user)) {
            return false;
        }

        return $this->hostsCanAddParticipants;
    }

    private function canTransferParticipants(OfferInterface $offer, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        // Transfer surfaces participant data and mutates attendances, so it
        // requires participant-view access. It is gated by its own flag rather
        // than participants.add: moving existing participants between a host's
        // own offers is a distinct, more contained capability than admitting
        // brand-new registrations.
        if (false === $this->canViewParticipants($offer, $user)) {
            return false;
        }

        return $this->hostsCanTransferParticipants;
    }

    private function canEditParticipants(OfferInterface $offer, User $user): bool
    {
        if ($this->security->isGranted('ROLE_ADMIN')) {
            return true;
        }

        if (false === $this->canView($offer, $user)) {
            return false;
        }

        return $this->hostsCanEditParticipantList;
    }
}
