<?php
declare(strict_types=1);
$root = dirname(__DIR__);
@include_once $root.'/config/site.php';
$cfg  = function_exists('site_config') ? site_config() : [];
$fav  = $cfg['favicon_url'] ?? '/assets/img/favicon.png';
$ts   = (int)($cfg['updated_at'] ?? time());

header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache'); header('Expires: 0');

if (preg_match('#^https?://#i', $fav)) {
  $sep = (strpos($fav,'?')!==false ? '&' : '?');
  header('Location: '.$fav.$sep.'v='.$ts, true, 302); exit;
}

$file = $root.$fav;
if (!is_file($file)) {
  // fallbacks
  if (is_file($root.'/assets/img/favicon.png')) {
    $file = $root.'/assets/img/favicon.png';
  } else {
    $file = $root.'/assets/img/logo.svg';
  }
}

$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$map = ['ico'=>'image/x-icon','png'=>'image/png','jpg'=>'image/jpeg','jpeg'=>'image/jpeg','webp'=>'image/webp','svg'=>'image/svg+xml'];
header('Content-Type: '.($map[$ext] ?? 'image/x-icon'));
readfile($file);
