<?php

declare(strict_types=1);

namespace App\Support;

final class BlogHomepageDisplay
{
    public const MIN_ARTICLE_COUNT = 4000;

    /** @var int Inclusive upper spread above {@see MIN_ARTICLE_COUNT} (floor + [0, SPREAD]) */
    private const SYNTHETIC_SPREAD = 11_000;

    /**
     * Homepage category/tag counts: never below {@see MIN_ARTICLE_COUNT}, and not identical for every row.
     * Uses a stable synthetic floor from {@see $entropyKey} when published count is lower.
     */
    public static function articleCount(int $publishedCount, string $entropyKey): int
    {
        $syntheticFloor = self::syntheticFloor($entropyKey);

        return max($publishedCount, $syntheticFloor);
    }

    /**
     * Deterministic display floor in [MIN_ARTICLE_COUNT, MIN_ARTICLE_COUNT + SYNTHETIC_SPREAD - 1].
     */
    public static function syntheticFloor(string $entropyKey): int
    {
        $u = (int) sprintf('%u', crc32($entropyKey));

        return self::MIN_ARTICLE_COUNT + ($u % self::SYNTHETIC_SPREAD);
    }
}
