package handlers

import (
	"net/http"
	"os"
	"strings"
)

// ServeSitemapFiles expõe data/sitemaps/ em /sitemaps/ para visualização no navegador.
func ServeSitemapFiles() http.Handler {
	dir := "data/sitemaps"
	if _, err := os.Stat(dir); err != nil {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			http.NotFound(w, r)
		})
	}
	inner := http.FileServer(http.Dir(dir))
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasSuffix(r.URL.Path, ".xml") {
			w.Header().Set("Content-Type", "application/xml; charset=utf-8")
		}
		http.StripPrefix("/sitemaps/", inner).ServeHTTP(w, r)
	})
}
