<?php

// app/Views/_helpers.php

if (!function_exists('h')) {
    function h($s): string
    {
        return htmlspecialchars((string)$s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    }
}

function app_version(): string
{
    static $version = null;
    if ($version !== null) {
        return $version;
    }
    $path = dirname(__DIR__, 2) . '/VERSION';
    $value = is_file($path) ? trim((string) file_get_contents($path)) : '';
    return $version = ($value !== '' ? $value : 'développement');
}

function status_badge(string $st): array
{
    $st = strtolower(trim($st));
    return match ($st) {
        'refuge' => ['bg-blue-lt',t('status.refuge')],
        'fa' => ['bg-cyan-lt',t('status.fa')],
        'fa_permanente' => ['bg-indigo-lt',t('status.fa_permanente')],
        'chat_libre' => ['bg-green-lt text-green','🌿 ' . t('status.chat_libre')],
        'quarantaine' => ['bg-yellow-lt',t('status.quarantaine')],
        'soin' => ['bg-red-lt',t('status.soin')],
        'isolation' => ['bg-orange-lt',t('status.isolation')],
        'hospitalise','hospitalisé' => ['bg-red-lt',t('status.hospitalise')],
        'reserve','réservé' => ['bg-purple-lt',t('status.reserve')],
        'adopte','adopté' => ['bg-green-lt',t('status.adopte')],
        'decede','décédé' => ['bg-dark-lt',t('status.decede')],
        'enfui' => ['bg-secondary-lt',t('status.enfui')],
        default => ['bg-secondary-lt', $st !== '' ? $st : '—'],
    };
}

function species_label(?string $species): string
{
    if (class_exists('SpeciesService')) {
        return SpeciesService::label($species);
    }
    return match (strtolower(trim((string)$species))) {
        'cat', 'chat' => t('species.cat'),
        'dog', 'chien' => t('species.dog'),
        'nac' => 'NAC',
        '' => '—',
        default => ucfirst(trim((string)$species)),
    };
}

function coat_swatch_style(?string $coat): string
{
    return match (mb_strtolower(trim((string)$coat), 'UTF-8')) {
        'bicolore noir / blanc' => 'background:linear-gradient(135deg,#252525 0 50%,#fff 50% 100%)',
        'bicolore gris / blanc' => 'background:linear-gradient(135deg,#8b95a1 0 50%,#fff 50% 100%)',
        'bicolore roux / blanc' => 'background:linear-gradient(135deg,#d9822b 0 50%,#fff 50% 100%)',
        'bicolore tigré et blanc' => 'background:linear-gradient(90deg,transparent 0 55%,#fff 55% 100%),repeating-linear-gradient(135deg,#8a6848 0 5px,#3f3328 5px 8px)',
        'gris' => 'background:#8b95a1',
        'roux' => 'background:#d9822b',
        'noir' => 'background:#252525',
        'chocolat' => 'background:#6f4e37',
        'blanc' => 'background:#fff',
        'tricolore / calico' => 'background:conic-gradient(#252525 0 33%,#fff 33% 66%,#d9822b 66% 100%)',
        'écaille de tortue' => 'background:conic-gradient(#2d241f 0 28%,#b86326 28% 48%,#171513 48% 72%,#d18a45 72% 100%)',
        'colourpoint / siamois' => 'background:radial-gradient(circle at center,#f2dfbb 0 48%,#654b3c 52% 100%)',
        'tigré / tabby' => 'background:repeating-linear-gradient(135deg,#8a6848 0 5px,#3f3328 5px 8px)',
        default => 'background:#d9dde3',
    };
}

function sort_th(string $label, string $key, string $sort, string $dir, string $path = '/animals'): string
{
    $nextDir = ($sort === $key && strtoupper($dir) === 'ASC') ? 'desc' : 'asc';
    $arrow = ($sort === $key) ? (strtoupper($dir) === 'ASC' ? ' ↑' : ' ↓') : '';
    $qs = $_GET;
    $qs['sort'] = $key;
    $qs['dir'] = $nextDir;
    $href = $path . '?' . http_build_query($qs);
    return '<a class="text-reset text-decoration-none" href="' . htmlspecialchars($href, ENT_QUOTES) . '">' . $label . $arrow . '</a>';

}

function sortLink($column, $label, $sort, $dir, $year, $q, $species)
{
    $newDir = ($sort === $column && $dir === 'ASC') ? 'desc' : 'asc';

    $arrow = '';
    if ($sort === $column) {
        $arrow = $dir === 'ASC' ? ' ↑' : ' ↓';
    }

    return '<a href="?year=' . $year . '&q=' . urlencode($q) . '&species=' . urlencode($species) . '&sort=' . $column . '&dir=' . $newDir . '">' . $label . $arrow . '</a>';
}
