<?php
require_once __DIR__ . '/includes/seo.php';
header('Content-Type: application/xml; charset=UTF-8');
$base = rtrim(site_base_url(), '/');
$urls = [['loc'=>$base.'/','changefreq'=>'daily','priority'=>'1.0']];
foreach (read_json('categories', []) as $cat) {
  foreach (read_json('tools', []) as $tool) {
    if (($tool['category_id'] ?? '') === ($cat['id'] ?? '') && ($tool['status'] ?? 'active') === 'active' && empty($tool['external'])) {
      $url = $tool['url'] ?? '';
      if ($url !== '' && str_starts_with($url, '/')) $urls[]=['loc'=>$base.$url,'changefreq'=>'weekly','priority'=>'0.7'];
    }
  }
}
$seen=[]; echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach ($urls as $u) { if(isset($seen[$u['loc']])) continue; $seen[$u['loc']]=1; echo '<url><loc>'.htmlspecialchars($u['loc'],ENT_XML1,'UTF-8').'</loc><changefreq>'.$u['changefreq'].'</changefreq><priority>'.$u['priority'].'</priority></url>'; }
echo '</urlset>';
