package logger

import (
	"context"
	"log/slog"
	"os"
	"strings"
)

var level = slog.LevelInfo

func init() {
	env := os.Getenv("LLM_PROXY_LOG_LEVEL")
	switch strings.ToLower(env) {
	case "debug":
		level = slog.LevelDebug
	case "warn":
		level = slog.LevelWarn
	case "error":
		level = slog.LevelError
	}

	h := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
		Level: level,
	})
	slog.SetDefault(slog.New(h))
}

// Warn is a helper to emit structured warnings.
func Warn(msg string, args ...any) {
	slog.Warn(msg, args...)
}

// Error is a helper to emit structured errors.
func Error(msg string, args ...any) {
	slog.Error(msg, args...)
}

// Debug is a helper to emit structured debug messages.
func Debug(msg string, args ...any) {
	slog.Debug(msg, args...)
}

// Ctx wraps slog with context.
func Ctx(ctx context.Context) *slog.Logger {
	return slog.Default()
}
