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

use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;

/**
 * Single source of truth for the legacy `offer_list` reader's `auto_item`
 * segment: `{base58-uuid}-{slug}` (bare `{base58-uuid}` when the offer has no
 * alias).
 *
 * Aliases are no longer unique — variants share a slug — so the base58 UUID is
 * the authoritative, stable lookup key and the slug is the decorative tail
 * (like StackOverflow's `/questions/{id}/{slug}`). The reader 301s any
 * non-canonical form (bare UUID, stale slug, or a pre-migration plain-alias
 * bookmark) to this one.
 */
final class OfferReaderUrl
{
    public static function autoItem(OfferInterface $offer): string
    {
        $base58 = $offer->getUuid()->toBase58();
        $slug = (string) $offer->getAlias();

        return '' !== $slug ? $base58.'-'.$slug : $base58;
    }
}
