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

use Ferienpass\AdminBundle\Event\ParticipantListEvent;
use Ferienpass\CoreBundle\Application\ExtraQuestion;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener(priority: -10)]
class ParticipantListListener
{
    public function __invoke(ParticipantListEvent $event): void
    {
        $columns = $event->getColumns();
        $offer = $event->getOffer();

        if ($offer->isCollectDateOfBirth()) {
            $columns['dateOfBirth'] = '{{ item.participant and item.participant.dateOfBirth ? item.participant.dateOfBirth|date("d.m.Y") : "-" }}';
        }

        foreach ($offer->getApplicationExtra() ?? [] as $extra) {
            if ('yn' === $extra['type']) {
                $columns[$extra['question']] = "{{ 'y' == item.extra('{$extra['uuid']}') ? 'Ja' : 'Nein' }}";
            } elseif (ExtraQuestion::TYPE_FILE === $extra['type']) {
                $columns[$extra['question']] = $this->fileCell((string) $extra['uuid']);
            } else {
                $columns[$extra['question']] = "{{ item.extra('{$extra['uuid']}') }}";
            }
        }

        $event->setColumns($columns);
    }

    /**
     * A file answer opens the combined view/replace drawer
     * ({@see \Ferienpass\AdminBundle\Controller\AttendanceExtraFileController}).
     * The link is rendered whether or not a file was uploaded: with one it is the
     * way to look at and download it, without one it is how the team adds the file
     * they got by other means (over the phone, at the counter).
     *
     * The cell deliberately does NOT print the filename. It comes from a phone or a
     * scanner, so it is long, meaningless and of wildly varying length
     * ("IMG_20260819_121314.jpg", "Scan 12.08.2026, 14-02.pdf") — a column of those
     * is noise that pushes every other column off screen. The name is right there in
     * the drawer, where there is room for it.
     */
    private function fileCell(string $uuid): string
    {
        // Ids travel base58 in the admin URLs, never the stored RFC 4122 form.
        $question = ExtraQuestion::urlId($uuid);

        // language=twig
        return '
{% set file = item.extraFile("'.$uuid.'") %}
<a href="{{ path("admin_attendance_file", { uuid: item.uuid.toBase58, question: "'.$question.'" }) }}" data-turbo-frame="slideover"
   class="inline-flex items-center gap-x-1.5 rounded-md px-1.5 py-1 -mx-1.5 -my-1 text-sm transition-colors hover:bg-gray-100 dark:hover:bg-gray-800">
    {% if file %}
        {{ ux_icon(file.isPdf ? "ph:file-pdf" : "ph:image", { class: "size-4 shrink-0 text-gray-400" }) }}
        <span class="font-medium text-gray-900 dark:text-gray-100">Datei anzeigen</span>
    {% else %}
        {{ ux_icon("ph:upload-simple", { class: "size-4 shrink-0 text-gray-400" }) }}
        <span class="text-gray-500 dark:text-gray-400">Keine Datei</span>
    {% endif %}
</a>';
    }
}
