package middleware

import (
	"net/http"
	"strings"
)

func SecurityHeaders(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		isDocsRequest := strings.HasPrefix(r.URL.Path, "/docs/")
		w.Header().Set("X-Content-Type-Options", "nosniff")
		w.Header().Set("X-Frame-Options", "DENY")
		w.Header().Set("X-XSS-Protection", "1; mode=block")
		w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
		if isDocsRequest {
			w.Header().Set("Content-Security-Policy", "default-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'")
		} else {
			// Content-Security-Policy can be very strict, maybe good to start lenient or specific.
			// For an API, it's less critical than a frontend serving HTML, but good practice.
			w.Header().Set("Content-Security-Policy", "default-src 'none'")
		}
		w.Header().Set("Cache-Control", "no-store, max-age=0")

		next.ServeHTTP(w, r)
	})
}
