<?php
/**
 * RC Workspace — Dynamic Sitemap Generator
 * Place at: public_html/sitemap.php
 * Access at: https://www.rcworkspace.company/sitemap.php
 *
 * This auto-scans all index.php pages across the site
 * and outputs a valid XML sitemap with priorities.
 * Submit https://www.rcworkspace.company/sitemap.php to GSC
 * OR keep using sitemap.xml and run this to regenerate it.
 */

$BASE_URL = 'https://www.rcworkspace.company';
$DOC_ROOT = rtrim($_SERVER['DOCUMENT_ROOT'], '/');

// ─── Priority map — set per directory pattern ───────────────────
function getPriority(string $path): string {
    if ($path === '/') return '1.0';
    if (preg_match('#^/(office|warehouse)/$#', $path)) return '0.95';
    if (preg_match('#^/services/(office-fit-out|warehouse-fit-out|commercial-interior)/#', $path)) return '0.90';
    if (preg_match('#^/(office|warehouse)/#', $path)) return '0.85';
    if (preg_match('#^/services/#', $path)) return '0.80';
    if (preg_match('#^/case-study/#', $path)) return '0.80';
    if (preg_match('#^/free-pre-lease-audit/#', $path)) return '0.90';
    if (preg_match('#^/blog/#', $path)) return '0.75';
    if (preg_match('#^/industries/#', $path)) return '0.70';
    if (preg_match('#^/portfolio/#', $path)) return '0.80';
    return '0.60';
}

function getChangefreq(string $path): string {
    if ($path === '/') return 'weekly';
    if (preg_match('#^/blog/#', $path)) return 'monthly';
    return 'monthly';
}

// ─── Directories to EXCLUDE from sitemap ────────────────────────
$EXCLUDE = [
    '/assets/',
    '/admin/',
    '/config/',
    '/content/',
    '/portal/',
    '/projects/',
    '/rcportal/',
    '/vendor/',
    '/node_modules/',
];

// ─── Scan all index.php files recursively ───────────────────────
$urls = [];

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($DOC_ROOT, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($iterator as $file) {
    if ($file->getFilename() !== 'index.php') continue;

    $fullPath = $file->getPathname();
    $relativePath = str_replace($DOC_ROOT, '', $fullPath);
    $dirPath = str_replace('/index.php', '/', $relativePath);

    // Normalize root
    if ($dirPath === '/index.php') $dirPath = '/';
    if (!str_ends_with($dirPath, '/')) $dirPath .= '/';

    // Skip excluded directories
    $skip = false;
    foreach ($EXCLUDE as $ex) {
        if (str_starts_with($dirPath, $ex)) { $skip = true; break; }
    }
    if ($skip) continue;

    // Skip sitemap.php itself and other utility files
    if (str_contains($fullPath, 'sitemap')) continue;

    $lastmod = date('Y-m-d', filemtime($fullPath));

    $urls[] = [
        'loc'        => $BASE_URL . $dirPath,
        'lastmod'    => $lastmod,
        'changefreq' => getChangefreq($dirPath),
        'priority'   => getPriority($dirPath),
    ];
}

// Sort: homepage first, then by priority desc, then alpha
usort($urls, function($a, $b) {
    if ($a['loc'] === 'https://www.rcworkspace.company/') return -1;
    if ($b['loc'] === 'https://www.rcworkspace.company/') return 1;
    if ($a['priority'] !== $b['priority']) return strcmp($b['priority'], $a['priority']);
    return strcmp($a['loc'], $b['loc']);
});

// ─── Output ─────────────────────────────────────────────────────
// If ?save=1 is passed, write to sitemap.xml instead of outputting
$save = isset($_GET['save']) && $_GET['save'] === '1';

$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

foreach ($urls as $url) {
    $xml .= "  <url>\n";
    $xml .= "    <loc>" . htmlspecialchars($url['loc']) . "</loc>\n";
    $xml .= "    <lastmod>{$url['lastmod']}</lastmod>\n";
    $xml .= "    <changefreq>{$url['changefreq']}</changefreq>\n";
    $xml .= "    <priority>{$url['priority']}</priority>\n";
    $xml .= "  </url>\n";
}

$xml .= '</urlset>';

if ($save) {
    // Write sitemap.xml to disk
    $sitemapPath = $DOC_ROOT . '/sitemap.xml';
    file_put_contents($sitemapPath, $xml);
    echo "✅ sitemap.xml updated — " . count($urls) . " URLs written to $sitemapPath\n";
    echo "\nURLs included:\n";
    foreach ($urls as $url) {
        echo "  [{$url['priority']}] {$url['loc']}\n";
    }
} else {
    // Output as XML
    header('Content-Type: application/xml; charset=utf-8');
    echo $xml;
}