<?php
declare(strict_types=1);
$crm=json_decode((string)file_get_contents(__DIR__.'/assets/data/crm-public.json'),true) ?: [];
$origin=rtrim((string)($crm['projectDomain']??''),'/');
if(!preg_match('~^https?://[a-z0-9.-]+(?::[0-9]+)?$~i',$origin)){http_response_code(503);exit;}
if(parse_url($_SERVER['REQUEST_URI']??'',PHP_URL_PATH)==='/robots.txt'){
 header('Content-Type: text/plain; charset=UTF-8');
 echo "User-agent: *\nAllow: /\nDisallow: /thankyou\nDisallow: /assets/data/\nDisallow: /.energy-pages/\nDisallow: /.energy-source/\nSitemap: ".$origin."/sitemap.xml\n";exit;
}
$routes=json_decode((string)file_get_contents(__DIR__.'/.energy-pages/routes.json'),true) ?: [];
header('Content-Type: application/xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($routes as $path=>$page)if(!in_array($path,['404','thankyou'],true))echo '<url><loc>'.htmlspecialchars($origin.'/'.$path,ENT_XML1|ENT_QUOTES,'UTF-8').'</loc></url>';
echo '</urlset>';
