<?php
declare(strict_types=1);

/**
 * Gerçekçi sosyal profil foto havuzu (randomuser.me — ücretsiz demo portreleri).
 * Shutterstock telifli; oradan çekilmez.
 *
 * @return list<string>
 */
function avatar_photo_pool(): array
{
    static $pool = null;
    if ($pool !== null) {
        return $pool;
    }

    $pool = [];
    // 50 kadın + 50 erkek = 100 karışık portre
    for ($i = 0; $i < 50; $i++) {
        $pool[] = 'https://randomuser.me/api/portraits/women/' . $i . '.jpg';
        $pool[] = 'https://randomuser.me/api/portraits/men/' . $i . '.jpg';
    }
    return $pool;
}

/** @return list<string> */
function avatar_gradient_palette(): array
{
    return [
        'linear-gradient(145deg, #ff8a4c, #ff4d8d)',
        'linear-gradient(145deg, #ff6b9d, #a855f7)',
        'linear-gradient(145deg, #8b5cf6, #1e3a8a)',
        'linear-gradient(145deg, #22c55e, #0ea5e9)',
        'linear-gradient(145deg, #f59e0b, #ef4444)',
        'linear-gradient(145deg, #06b6d4, #6366f1)',
        'linear-gradient(145deg, #ec4899, #f97316)',
        'linear-gradient(145deg, #14b8a6, #3b82f6)',
        'linear-gradient(145deg, #fb7185, #c026d3)',
        'linear-gradient(145deg, #38bdf8, #6366f1)',
    ];
}

function avatar_random_seed(): string
{
    return bin2hex(random_bytes(6));
}

function avatar_seed_of(array $item, string $fallback = ''): string
{
    $seed = trim((string) ($item['avatar_seed'] ?? ''));
    if ($seed !== '') {
        return $seed;
    }
    if ($fallback !== '') {
        return $fallback;
    }
    $id = trim((string) ($item['id'] ?? ''));
    if ($id !== '') {
        return $id;
    }
    $name = trim((string) ($item['guest_name'] ?? $item['name'] ?? 'guest'));
    return $name !== '' ? $name : 'guest';
}

/** Aynı seed → aynı tip ve aynı foto/siluet. */
function avatar_meta(string $seed, string $role = 'user'): array
{
    $seed = trim($seed) !== '' ? $seed : 'guest';
    $hash = (int) sprintf('%u', crc32($seed));
    $pool = avatar_photo_pool();
    $gradients = avatar_gradient_palette();

    // Satıcıda biraz daha sık foto; yorumcuda ~%45 foto / %55 siluet
    $photoChance = $role === 'seller' ? 55 : 45;
    $usePhoto = ($hash % 100) < $photoChance;

    return [
        'seed' => $seed,
        'type' => $usePhoto ? 'photo' : 'silhouette',
        'gradient' => $gradients[$hash % count($gradients)],
        'photo_url' => $pool[$hash % count($pool)],
    ];
}

function avatar_silhouette_svg(): string
{
    return '<svg class="avatar-silhouette" viewBox="0 0 24 24" aria-hidden="true">'
        . '<path fill="#fff" d="M12 12c2.7 0 4.8-2.1 4.8-4.8S14.7 2.4 12 2.4 7.2 4.5 7.2 7.2 9.3 12 12 12zm0 2.4c-3.2 0-9.6 1.6-9.6 4.8v1.2h19.2V19.2c0-3.2-6.4-4.8-9.6-4.8z"/>'
        . '</svg>';
}

function avatar_img_html(string $seed, string $class = 'avatar', string $alt = '', string $role = 'user'): string
{
    $meta = avatar_meta($seed, $role);

    if ($meta['type'] === 'photo') {
        return '<div class="' . e($class) . ' diamond has-photo" aria-hidden="true">'
            . '<span class="diamond-inner">'
            . '<img src="' . e($meta['photo_url']) . '" alt="' . e($alt) . '" loading="lazy" width="48" height="48">'
            . '</span>'
            . '</div>';
    }

    return '<div class="' . e($class) . ' diamond is-silhouette" style="--diamond-bg:' . e($meta['gradient']) . '" aria-hidden="true">'
        . '<span class="diamond-inner">' . avatar_silhouette_svg() . '</span>'
        . '</div>';
}

function avatar_url(string $seed, string $role = 'user'): string
{
    return avatar_meta($seed, $role)['photo_url'];
}
