<?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\EventListener\Workflow;

use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Enum\AttendanceStatus;
use Symfony\Component\Workflow\Attribute\AsEnteredListener;
use Symfony\Component\Workflow\Event\EnteredEvent;

/**
 * Taking a participant off the confirmed list — back onto the waiting list
 * ({@see \Ferienpass\CoreBundle\Enum\AttendanceTransition::WAITLIST}) or back to
 * "neu" ({@see \Ferienpass\CoreBundle\Enum\AttendanceTransition::RESET}) — ends
 * the liability an earlier confirmation created, exactly as a rejection does.
 * Without this the fee would stay owed for a child that does not take part.
 *
 * Unlike the confirming direction this ignores the `commit` context: not billing
 * yet is a decision the team can defer, but keeping a bill for a non-participant
 * is never right. Money already collected is untouched — the attendance then
 * surfaces as refundable (payable = 0 with a positive balance), which is the same
 * state a cancelled offer produces.
 */
#[AsEnteredListener(workflow: 'attendance', place: AttendanceStatus::WAITLISTED)]
#[AsEnteredListener(workflow: 'attendance', place: AttendanceStatus::WAITING)]
class AttendanceUnconfirmTransactionListener
{
    public function __invoke(EnteredEvent $event): void
    {
        $attendance = $event->getSubject();
        if (!$attendance instanceof Attendance) {
            throw new \RuntimeException('Unexpected event subject');
        }

        $attendance->setPayable(false);
    }
}
