<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

  <%# Homepage %>
  <url>
    <loc><%= @protocol %><%= @host %>/</loc>
    <lastmod><%= @website.updated_at.strftime('%Y-%m-%d') %></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>

  <%# Properties for sale %>
  <% @properties_for_sale.each do |property| %>
  <url>
    <loc><%= @protocol %><%= @host %>/properties/for-sale/<%= property.slug.presence || property.id %>/<%= property.title.to_s.parameterize %></loc>
    <lastmod><%= property.updated_at.strftime('%Y-%m-%d') %></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
    <%# Include property images (up to 5 per URL) - photos are pre-loaded %>
    <% property.prop_photos.sort_by(&:sort_order).first(5).each do |photo| %>
    <% photo_url = photo.image_url rescue nil %>
    <% if photo_url.present? %>
    <image:image>
      <image:loc><%= photo_url %></image:loc>
      <image:title><%= property.title.to_s.truncate(100) %></image:title>
      <image:caption><%= [property.title, property.city].compact.join(' - ').truncate(200) %></image:caption>
    </image:image>
    <% end %>
    <% end %>
  </url>
  <% end %>

  <%# Properties for rent %>
  <% @properties_for_rent.each do |property| %>
  <url>
    <loc><%= @protocol %><%= @host %>/properties/for-rent/<%= property.slug.presence || property.id %>/<%= property.title.to_s.parameterize %></loc>
    <lastmod><%= property.updated_at.strftime('%Y-%m-%d') %></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
    <%# Include property images (up to 5 per URL) - photos are pre-loaded %>
    <% property.prop_photos.sort_by(&:sort_order).first(5).each do |photo| %>
    <% photo_url = photo.image_url rescue nil %>
    <% if photo_url.present? %>
    <image:image>
      <image:loc><%= photo_url %></image:loc>
      <image:title><%= property.title.to_s.truncate(100) %></image:title>
      <image:caption><%= [property.title, property.city].compact.join(' - ').truncate(200) %></image:caption>
    </image:image>
    <% end %>
    <% end %>
  </url>
  <% end %>

  <%# Static pages %>
  <% @pages.each do |page| %>
  <url>
    <loc><%= @protocol %><%= @host %>/p/<%= page.slug %></loc>
    <lastmod><%= page.updated_at.strftime('%Y-%m-%d') %></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.6</priority>
  </url>
  <% end %>

  <%# Property listing pages %>
  <url>
    <loc><%= @protocol %><%= @host %>/properties/for-sale</loc>
    <lastmod><%= Date.today.strftime('%Y-%m-%d') %></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>

  <url>
    <loc><%= @protocol %><%= @host %>/properties/for-rent</loc>
    <lastmod><%= Date.today.strftime('%Y-%m-%d') %></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>

</urlset>
