<?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\AdminBundle\Components;

use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Entity\PaymentItem;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;

#[AsLiveComponent(route: 'live_component_admin')]
class AttendancePayments extends AbstractController
{
    use DefaultActionTrait;

    #[LiveProp]
    public Attendance $attendance;

    public function __construct(public readonly EventDispatcherInterface $dispatcher)
    {
    }

    #[ExposeInTemplate]
    public function getPayments(): array
    {
        $payments = [];
        foreach ($this->attendance->getPaymentItems() as $paymentItem) {
            /** @var PaymentItem $paymentItem */
            $payment = $paymentItem->getPayment();
            if (!\in_array($payment, $payments, true)) {
                $payments[] = $payment;
            }
        }

        return $payments;
    }
}
