package logging

type LogLevel string

const (
	LogLevelTrace   LogLevel = "TRACE"
	LogLevelDebug   LogLevel = "DEBUG"
	LogLevelInfo    LogLevel = "INFO"
	LogLevelWarning LogLevel = "WARN"
	LogLevelError   LogLevel = "ERROR"
	LogLevelFatal   LogLevel = "FATAL"
)

// MatchFilter is called on a filter (l) with a target level to match on the filter. Match
// means returning true of the target is greater than OR EQUAL TO the filter level. They order
// by scale of magnitude.
func (filter LogLevel) MatchFilter(target LogLevel) bool {
	// Define severity levels (higher number = more severe)
	severity := map[LogLevel]int{
		LogLevelTrace:   0,
		LogLevelDebug:   1,
		LogLevelInfo:    2,
		LogLevelWarning: 3,
		LogLevelError:   4,
		LogLevelFatal:   5,
	}

	filterSeverity, filterOk := severity[filter]
	targetSeverity, targetOk := severity[target]

	if !filterOk || !targetOk {
		return false
	}

	return targetSeverity >= filterSeverity
}

const (
	// Background colors
	BgBlack   = "\033[40m"
	BgRed     = "\033[41m"
	BgGreen   = "\033[42m"
	BgYellow  = "\033[43m"
	BgBlue    = "\033[44m"
	BgMagenta = "\033[45m"
	BgCyan    = "\033[46m"
	BgWhite   = "\033[47m"

	// Reset
	Reset = "\033[0m"
)

type Logger interface {
	Log(level LogLevel, format string, v ...any)
}

// LogAll takes all of the inputs for a single logger and executes the logging operation
// on each of the loggers (logs) provided. This is just a convince function.
func LogAll(logs []Logger, level LogLevel, format string, v ...any) {
	for _, log := range logs {
		log.Log(level, format, v...)
	}
}
