<?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\ConsentManager;

use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\User;

class ConsentRequirementChecker
{
    public function __construct(private ConsentManager $consentManager)
    {
    }

    /**
     * @return ConsentState[]
     */
    public function getMustSignFirstConsents(OfferInterface $offer, array $participants, ?User $user): array
    {
        $requiredConsents = $this->consentManager->forOfferAndParticipants($offer, $participants, $user);

        return array_filter($requiredConsents, static function (ConsentState $state) {
            // Exclude participant-level offer consents from the "must sign first" list
            // as they are typically handled during the individual participant application step
            if (!$state->getConsentForm()?->isUserLevel()) {
                return false;
            }

            return $state->isOutstanding();
        });
    }

    public function hasUnsignedConsents(OfferInterface $offer, array $participants, ?User $user): bool
    {
        return [] !== $this->getMustSignFirstConsents($offer, $participants, $user);
    }
}
