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

use Ferienpass\CoreBundle\Entity\ConversationItem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Turns a {@see ConversationItem} into the admin URL of the page that hosts its
 * conversation (the subject's show page). Shared by the header notification bell
 * (relative, same-host redirect) and the comment notification email (absolute,
 * so the link works from an inbox) — one place for the subject → route mapping.
 */
final readonly class ConversationItemUrlGenerator
{
    public function __construct(private UrlGeneratorInterface $urlGenerator)
    {
    }

    /**
     * The admin URL of the subject's show page — where the conversation lives.
     * Returns null for subjects without a dedicated show page (e.g. editions).
     */
    public function adminUrl(ConversationItem $item, int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): ?string
    {
        $subject = $item->getSubject();

        try {
            return match ($item->getSubjectType()) {
                'offer' => $this->urlGenerator->generate('admin_offers_show', ['uuid' => $subject->getUuid()->toBase58()], $referenceType),
                'attendance' => $this->urlGenerator->generate('admin_offers_show', ['uuid' => $subject->getOffer()->getUuid()->toBase58()], $referenceType),
                'participant' => $this->urlGenerator->generate('admin_participants_show', ['uuid' => $subject->getUuid()->toBase58()], $referenceType),
                'account' => $this->urlGenerator->generate('admin_accounts_show', ['uuid' => $subject->getUuid()->toBase58()], $referenceType),
                'host' => $this->urlGenerator->generate('admin_hosts_show', ['uuid' => $subject->getUuid()->toBase58()], $referenceType),
                default => null,
            };
        } catch (\Throwable) {
            return null;
        }
    }

    /**
     * The admin URL of the recipient's own notification-preferences page (the
     * "Benachrichtigungen verwalten" footer link). Only meaningful for admins and
     * hosts — the people who actually hold an admin account.
     */
    public function settingsUrl(int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): ?string
    {
        try {
            return $this->urlGenerator->generate('admin_user_index', [], $referenceType);
        } catch (\Throwable) {
            return null;
        }
    }
}
