// backend/internal/debuglog.go
package internal

import (
	"log"
	"net/http"
	"os"
	"strings"
)

// Debug enables verbose request and state logging.
var Debug = os.Getenv("PEGASUS_DEBUG") == "1"

// DryRun makes mutating handlers log their intent instead of touching disk.
var DryRun = os.Getenv("PEGASUS_DRY_RUN") == "1"

// Logf writes a log line only when Debug is enabled.
func Logf(format string, args ...any) {
	if Debug {
		log.Printf(format, args...)
	}
}

// RedactHeaders clones a header map and masks sensitive values.
func RedactHeaders(h http.Header) http.Header {
	cp := http.Header{}
	for k, v := range h {
		kk := strings.ToLower(k)
		if kk == "cookie" || strings.HasPrefix(kk, "authorization") || strings.Contains(kk, "token") {
			cp[k] = []string{"<redacted>"}
		} else {
			cp[k] = append([]string(nil), v...)
		}
	}
	return cp
}
