<?php
require_once __DIR__ . "/config.php";

header("Content-Type: application/xml; charset=utf-8");

$baseUrl = "https://pingackzz.hr";

function xmlUrl(string $loc, ?string $lastmod = null, string $changefreq = "weekly", string $priority = "0.7"): string {
  $out  = "  <url>\n";
  $out .= "    <loc>" . htmlspecialchars($loc, ENT_XML1, "UTF-8") . "</loc>\n";
  if ($lastmod) {
    $out .= "    <lastmod>" . htmlspecialchars(date("Y-m-d", strtotime($lastmod)), ENT_XML1, "UTF-8") . "</lastmod>\n";
  }
  $out .= "    <changefreq>" . htmlspecialchars($changefreq, ENT_XML1, "UTF-8") . "</changefreq>\n";
  $out .= "    <priority>" . htmlspecialchars($priority, ENT_XML1, "UTF-8") . "</priority>\n";
  $out .= "  </url>\n";
  return $out;
}

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";

echo xmlUrl("$baseUrl/", null, "daily", "1.0");
echo xmlUrl("$baseUrl/galerija.php", null, "monthly", "0.6");
echo xmlUrl("$baseUrl/kontakt.php", null, "yearly", "0.4");

echo xmlUrl("$baseUrl/liga/vijesti.php", null, "daily", "0.9");
echo xmlUrl("$baseUrl/liga/oglasi.php", null, "daily", "0.8");
echo xmlUrl("$baseUrl/liga/poredak.php", null, "daily", "0.8");
echo xmlUrl("$baseUrl/liga/utakmice.php", null, "daily", "0.8");
echo xmlUrl("$baseUrl/liga/rang_liste.php", null, "weekly", "0.7");
echo xmlUrl("$baseUrl/liga/rang_global_bodovi.php?cycle=2025-2027", null, "weekly", "0.7");
echo xmlUrl("$baseUrl/liga/rang_elo_global.php?cycle=2025-2027", null, "weekly", "0.7");
echo xmlUrl("$baseUrl/liga/kup_kzz.php?cycle=2025-2027", null, "weekly", "0.7");

try {
  $stmt = $pdo->query("
    SELECT slug, updated_at, published_at, created_at
    FROM posts
    WHERE TYPE='NEWS' AND is_published=1 AND slug IS NOT NULL AND slug <> ''
    ORDER BY COALESCE(updated_at, published_at, created_at) DESC
  ");

  foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) {
    $lastmod = $r['updated_at'] ?: ($r['published_at'] ?: $r['created_at']);
    echo xmlUrl(
      "$baseUrl/liga/vijest.php?slug=" . urlencode($r['slug']),
      $lastmod,
      "monthly",
      "0.8"
    );
  }
} catch (Exception $e) {}

try {
  $stmt = $pdo->query("
    SELECT slug, created_at, expires_at
    FROM oglasi
    WHERE status='published'
      AND slug IS NOT NULL AND slug <> ''
      AND (expires_at IS NULL OR expires_at >= CURDATE())
    ORDER BY created_at DESC
  ");

  foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) {
    echo xmlUrl(
      "$baseUrl/liga/oglas.php?slug=" . urlencode($r['slug']),
      $r['created_at'],
      "weekly",
      "0.6"
    );
  }
} catch (Exception $e) {}

try {
  $stmt = $pdo->query("
    SELECT id, match_date
    FROM cup_matches
    ORDER BY COALESCE(match_date, created_at) DESC
  ");

  foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) {
    echo xmlUrl(
      "$baseUrl/liga/kup_match.php?id=" . (int)$r['id'],
      $r['match_date'],
      "monthly",
      "0.6"
    );
  }
} catch (Exception $e) {}

try {
  $stmt = $pdo->query("
    SELECT id
    FROM players
    ORDER BY player_name ASC
  ");

  foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) {
    echo xmlUrl(
      "$baseUrl/liga/player_detail.php?id=" . (int)$r['id'],
      null,
      "monthly",
      "0.5"
    );
  }
} catch (Exception $e) {}

echo "</urlset>";