<?php
require __DIR__.'/app/bootstrap.php';
require_once __DIR__.'/app/layout.php'; // Provides local_media_path() used to omit missing images.
header('Content-Type: application/xml; charset=UTF-8');
function xml(string $value): string { return htmlspecialchars($value,ENT_XML1|ENT_QUOTES,'UTF-8'); }
$entries=[];
$addPair=static function(string $faPath,string $enPath='',?string $lastmod=null,array $images=[])use(&$entries):void{
  $fa=url($faPath);$en=url($enPath!==''?$enPath:$faPath);
  foreach([['fa',$fa],['en',$en]] as [$lang,$loc])$entries[$loc]=['lang'=>$lang,'fa'=>$fa,'en'=>$en,'lastmod'=>$lastmod,'images'=>$images];
};
$addPair('?lang=fa','?lang=en');
foreach(['tours','hotels','services','contact','blog'] as $page)$addPair('?page='.$page.'&lang=fa','?page='.$page.'&lang=en');
foreach($pdo->query('SELECT page_key,updated_at FROM content_pages') as $row){$slug=rawurlencode($row['page_key']);$addPair('?page=content&slug='.$slug.'&lang=fa','?page=content&slug='.$slug.'&lang=en',$row['updated_at']?:null);}
$catalogGallery=[];$mediaQuery=$pdo->query("SELECT m.entity_type,m.entity_id,m.image_path FROM media m LEFT JOIN hotels h ON m.entity_type='hotel' AND h.id=m.entity_id LEFT JOIN tours t ON m.entity_type='tour' AND t.id=m.entity_id WHERE (m.entity_type='hotel' AND h.active=1) OR (m.entity_type='tour' AND t.active=1) ORDER BY m.entity_type,m.entity_id,m.sort_order,m.id");foreach($mediaQuery as $mediaRow)$catalogGallery[$mediaRow['entity_type']][(int)$mediaRow['entity_id']][]=$mediaRow['image_path'];
foreach(['tours'=>'tour','hotels'=>'hotel'] as $table=>$kind){
  $columns=$pdo->query("SHOW COLUMNS FROM `$table` LIKE 'updated_at'")->fetch();
  $dateColumn=$columns?'updated_at':null;
  $select='slug,cover_image'.($dateColumn?',updated_at':'');
  foreach($pdo->query("SELECT id,$select FROM `$table` WHERE active=1") as $row){
    $slug=rawurlencode($row['slug']);$image=trim((string)($row['cover_image']??''));$images=[];
    if($image!==''&&local_media_path($image,'')!=='')$images[]=$image;
    foreach($catalogGallery[$kind][(int)$row['id']]??[] as $galleryImage)if(local_media_path((string)$galleryImage,'')!=='')$images[]=$galleryImage;
    $lastmod=$dateColumn&&!empty($row['updated_at'])?$row['updated_at']:null;
    $addPair('?page='.$kind.'&slug='.$slug.'&lang=fa','?page='.$kind.'&slug='.$slug.'&lang=en',$lastmod,$images);
  }
}
$columns=$pdo->query("SHOW COLUMNS FROM blog_posts LIKE 'updated_at'")->fetch();
$dateSelect=$columns?',updated_at':'';
foreach($pdo->query("SELECT slug,cover_image,published_at$dateSelect FROM blog_posts WHERE status='published' AND published_at<=NOW() ORDER BY published_at DESC") as $post){
  $slug=rawurlencode($post['slug']);$images=[];$image=trim((string)($post['cover_image']??''));if($image!==''&&local_media_path($image,'')!=='')$images[]=$image;
  $lastmod=$columns&&!empty($post['updated_at'])?$post['updated_at']:null;
  $addPair('?page=blog&slug='.$slug.'&lang=fa','?page=blog&slug='.$slug.'&lang=en',$lastmod,$images);
}
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
foreach($entries as $loc=>$entry){
  echo '<url><loc>'.xml($loc).'</loc>';
  if(!empty($entry['lastmod'])){$time=strtotime((string)$entry['lastmod']);if($time!==false)echo '<lastmod>'.gmdate('c',$time).'</lastmod>';}
  echo '<xhtml:link rel="alternate" hreflang="fa" href="'.xml($entry['fa']).'"/><xhtml:link rel="alternate" hreflang="en" href="'.xml($entry['en']).'"/><xhtml:link rel="alternate" hreflang="x-default" href="'.xml($entry['fa']).'"/>';
  foreach($entry['images'] as $image)echo '<image:image><image:loc>'.xml(url($image)).'</image:loc></image:image>';
  echo '</url>';
}
echo '</urlset>';
