// Package verbose provides debug logging for all outbound requests (HTTP, exec, etc.).
// When enabled, it logs full request/response details to help debug connectivity
// and API issues.
//
// Controlled by:
//   - CP9_VERBOSE=1 environment variable
//   - cp9 verbose=true ticat env
//
// All outbound calls (HTTP requests, gRPC calls, external tool invocations) MUST
// use the helpers in this package so that verbose mode captures them uniformly.
package verbose

import (
	"log"
	"os"
	"sync/atomic"
)

var enabled atomic.Bool

func init() {
	if os.Getenv("CP9_VERBOSE") == "1" || os.Getenv("CP9_VERBOSE") == "true" {
		enabled.Store(true)
	}
}

// Enabled returns true when verbose mode is active.
func Enabled() bool { return enabled.Load() }

// SetEnabled controls verbose mode programmatically.
func SetEnabled(v bool) { enabled.Store(v) }

// Logf logs a verbose message when verbose mode is enabled.
// Messages are prefixed with "[verbose]" for easy filtering.
func Logf(format string, args ...any) {
	if enabled.Load() {
		log.Printf("[verbose] "+format, args...) //nolint:gosec // G706: format string is controlled by internal callers only
	}
}
