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

use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\OfferAttachment;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;

/**
 * Offer detail view. The legacy session-based "Merken" action has been
 * replaced by the client-side wishlists popover (see the wishlists Stimulus
 * controller), so this is no longer a LiveComponent.
 */
#[AsTwigComponent(template: '@Contao/fragment/offer_details.html.twig')]
class OfferDetails
{
    public OfferInterface $offer;

    public ?string $consentToken = null;

    public function __construct(
        private readonly VirtualFilesystemInterface $attachmentsStorage,
        #[Autowire(env: 'MAPBOX_TOKEN')]
        private readonly string $mapboxToken,
    ) {
    }

    #[ExposeInTemplate]
    public function mapboxToken(): string
    {
        return $this->mapboxToken;
    }

    #[ExposeInTemplate]
    public function attachments(): array
    {
        $items = [];
        foreach ($this->offer->getAttachments() as $attachment) {
            /** @var OfferAttachment $attachment */
            $items[] = $this->attachmentsStorage->get($attachment->getAttachment()->getUuid());
        }

        return array_filter($items);
    }
}
