<?php
// ====================================================================
// SITEMAP GENERATOR
// --------------------------------------------------------------------
// Membaca list.txt dan men-generate sitemap.xml.
// Setiap URL: https://gacwmwq.edu.pk/{brand_name_lowercase}
// (spasi di-encode %20, sama dengan konvensi URL di halaman).
//
// Penggunaan: php sitemap.php
// Output    : sitemap.xml
// ====================================================================

$config = [
    'brandsFile' => __DIR__ . '/list.txt',
    'output'     => __DIR__ . '/sitemap.xml',
    'baseUrl'    => 'https://gacwmwq.edu.pk',
];

$brandsFile = $config['brandsFile'];
if (!is_file($brandsFile)) {
    die("Error: File {$brandsFile} tidak ditemukan!\n");
}

$rows = file($brandsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$today    = date('Y-m-d');
$urlset   = [];
$urlset[] = '<?xml version="1.0" encoding="UTF-8"?>';
$urlset[] = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

$count = 0;
foreach ($rows as $line) {
    $d    = explode('|', $line);
    $name = trim($d[0]);
    if ($name === '') continue;

    // path = brand name lowercase (spasi -> %20)
    $path = rawurlencode(strtolower($name));
    $loc  = $config['baseUrl'] . '/' . $path;

    $urlset[] = '  <url>';
    $urlset[] = '    <loc>' . htmlspecialchars($loc, ENT_XML1) . '</loc>';
    $urlset[] = '    <lastmod>' . $today . '</lastmod>';
    $urlset[] = '    <changefreq>daily</changefreq>';
    $urlset[] = '    <priority>0.8</priority>';
    $urlset[] = '  </url>';
    $count++;
}

$urlset[] = '</urlset>';

file_put_contents($config['output'], implode("\n", $urlset) . "\n");

echo "Selesai! {$count} URL ditulis ke {$config['output']}\n";
