// Package log centralizes all user-facing output behind package-level
// functions backed by a private global logger. Normal output (actions, summary)
// goes to stdout; warnings and errors go to stderr. For a single-process CLI a
// global sink avoids threading a logger through every call site.
package log

import (
	"fmt"
	"io"
	"os"
)

// global is the process-wide logger. It defaults to the real stdout/stderr so the
// package is usable without explicit setup (e.g. in tests).
var global = &logger{stdout: os.Stdout, stderr: os.Stderr}

type logger struct {
	stdout  io.Writer
	stderr  io.Writer
	verbose bool
	quiet   bool
}

// Init configures the global logger's destinations. Call once at startup.
func Init(out, err io.Writer) {
	global.stdout = out
	global.stderr = err
}

// SetVerbose toggles whether commands include extra detail in their output.
func SetVerbose(v bool) { global.verbose = v }

// SetQuiet toggles whether normal output is suppressed. Quiet wins over verbose
// (see DESIGN §13): the two are points on one scale, quiet > verbose > default.
func SetQuiet(q bool) { global.quiet = q }

// Verbose reports whether verbose output is enabled. It folds the quiet override
// in, so callers never re-check quiet: quiet suppresses verbose detail.
func Verbose() bool { return global.verbose && !global.quiet }

// Print writes a normal output line to stdout. It is suppressed under quiet.
func Print(format string, args ...any) {
	if !global.quiet {
		fmt.Fprintf(global.stdout, format+"\n", args...)
	}
}

// PrintVerbose writes an extra-detail line to stdout, emitted only when verbose
// output is enabled (and not suppressed by quiet, per Verbose()).
func PrintVerbose(format string, args ...any) {
	if Verbose() {
		fmt.Fprintf(global.stdout, format+"\n", args...)
	}
}

// PrintImportant writes a line to stdout that survives quiet (e.g. the final
// summary).
func PrintImportant(format string, args ...any) {
	fmt.Fprintf(global.stdout, format+"\n", args...)
}

// Warn prints a warning to stderr. Warnings are never suppressed.
func Warn(format string, args ...any) {
	fmt.Fprintf(global.stderr, "Warning: "+format+"\n", args...)
}

// Error prints an error to stderr. Errors are never suppressed.
func Error(format string, args ...any) {
	fmt.Fprintf(global.stderr, "Error: "+format+"\n", args...)
}
