// internal/logger/logger.go
package logger

import (
	"io"
	"os"
	"time"

	"github.com/rs/zerolog"
)

var Log zerolog.Logger

// Init initializes the global logger.
// It configures console output, timestamps, and log level from environment.
func Init() {
	// Determine output writer
	consoleWriter := zerolog.ConsoleWriter{
		Out:        os.Stdout,
		TimeFormat: time.RFC3339,
	}

	// You can switch to JSON output by replacing with os.Stdout directly:
	// writer := os.Stdout

	// Create the global logger
	Log = zerolog.New(consoleWriter).
		Level(zerolog.TraceLevel).With().Caller().Logger().
		With().
		Timestamp().
		Logger()

	// Set log level from environment variable, default to InfoLevel
	level := zerolog.InfoLevel
	if lvlStr, ok := os.LookupEnv("LOG_LEVEL"); ok {
		if parsedLevel, err := zerolog.ParseLevel(lvlStr); err == nil {
			level = parsedLevel
		}
	}
	zerolog.SetGlobalLevel(level)
}

// SetOutput allows changing output (e.g., to a file)
func SetOutput(w io.Writer) {
	Log = Log.Output(w)
}
