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

use Ferienpass\AdminBundle\Event\ParticipantListEvent;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class ParticipantListDto
{
    public function __construct(private readonly EventDispatcherInterface $dispatcher)
    {
    }

    /**
     * @return array<string, string> where key is the table column and value is a twig equivalent of the cell value
     */
    public function adminViewColumns(OfferInterface $offer, Request $request): array
    {
        if ('slideover' === $request->headers->get('turbo-frame')) {
            return [
                // language=twig
                'name' => '<a href="{{ path("admin_participants_show", { uuid: item.participant.uuid.toBase58 }) }}" class="hover:bg-gray-100 dark:hover:bg-gray-800 hover:text-gray-900 dark:hover:text-gray-100 rounded-md px-1.5 py-1 -mx-1.5 -my-1 font-medium text-gray-900 dark:text-gray-100 transition-colors" data-turbo-frame="slideover">{{ item.participant ? item.participant.name : "Gelöschter Teilnehmer" }}</a>',
                // language=twig
                'age' => '
{% if not item.offer or not item.offer.dates|first or not item.offer.dates|first.begin %}
    {{ item.participant ? item.participant.age : "-" }}
{% else %}
    {{ item.participant ? item.participant.age(item.offer.dates|first.begin) : "-" }}
{% endif %}',
                // language=twig
                'status' => '
<twig:Admin:Badge attendance="{{ item.status }}" size="sm" class="-ml-2" />',
                // language=twig
                'consents' => '
<div class="flex flex-col gap-y-1">
    {% if item.participant %}
        {% for state in consent_states(item) %}
            <a href="{{ path("admin_consents_details", { form: state.consentForm.id, user: item.participant.user.id, participant: (state.participant ? state.participant.uuid : null), token: item.consentToken }) }}" data-turbo-frame="slideover" class="inline-flex items-center gap-x-1.5 text-xs text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800 hover:text-gray-900 dark:hover:text-gray-100 rounded-md px-1.5 py-1 -mx-1.5 -my-1 transition-colors" title="{{ state.title }}">
                <twig:Admin:ConsentStatusIcon :state="state" />
                <span class="truncate">{{ state.title }}</span>
            </a>
        {% endfor %}
    {% endif %}
</div>',
            ];
        }
        $columns = [
            // language=twig
            'name' => '<a href="{{ path("admin_participants_show", { uuid: item.participant.uuid.toBase58 }) }}" class="hover:bg-gray-100 dark:hover:bg-gray-800 hover:text-gray-900 dark:hover:text-gray-100 rounded-md px-1.5 py-1 -mx-1.5 -my-1 font-medium text-gray-900 dark:text-gray-100 transition-colors" data-turbo-frame="slideover">{{ item.participant ? item.participant.name : "Gelöschter Teilnehmer" }}</a>',
            // language=twig
            'age' => '
{% if not item.offer or not item.offer.dates|first or not item.offer.dates|first.begin %}
    {{ item.participant ? item.participant.age : "-" }}
{% else %}
    {{ item.participant ? item.participant.age(item.offer.dates|first.begin) : "-" }}
{% endif %}',
            // language=twig
            'email' => '{{ item.participant ? item.participant.email : "-" }}',
            // language=twig
            'mobile' => '{{ item.participant ? item.participant.mobile : "-" }}',
            // language=twig
            'phone' => '{{ item.participant ? item.participant.phone : "-" }}',
            // language=twig
            'status' => '
<a href="{{ path(\'admin_offers_participants_status\', { uuid: item.offer.uuid.toBase58, ids: item.id }) }}" data-turbo-frame="slideover" class="hover:underline">
    <twig:Admin:Badge attendance="{{ item.status }}" size="sm" class="-ml-2" />
</a>',
            // language=twig
            'consents' => '
<div class="flex flex-col gap-y-1">
    {% if item.participant %}
        {% for state in consent_states(item) %}
            <a href="{{ path("admin_consents_details", { form: state.consentForm.id, user: item.participant.user.id, participant: (state.participant ? state.participant.uuid : null), token: item.consentToken }) }}" data-turbo-frame="slideover" class="inline-flex items-center gap-x-1.5 text-xs text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800 hover:text-gray-900 dark:hover:text-gray-100 rounded-md px-1.5 py-1 -mx-1.5 -my-1 transition-colors" title="{{ state.title }}">
                <twig:Admin:ConsentStatusIcon :state="state" />
                <span class="truncate">{{ state.title }}</span>
            </a>
        {% endfor %}
    {% endif %}
</div>',
        ];

        $event = new ParticipantListEvent($offer, $columns);
        $this->dispatcher->dispatch($event);

        return $event->getColumns();
    }
}
