package logger

import (
	"errors"
	"log/slog"
	"os"
)

const (
	EnvLocal = "local"
	EnvDebug = "debug"
	EnvProd  = "production"
)

var ErrInvalidLogLevel = errors.New("invalid log level")

func New(env string) (*slog.Logger, error) {
	switch env {
	case EnvLocal:
		return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
			AddSource: true,
			Level:     slog.LevelInfo,
		})), nil

	case EnvDebug:
		return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
			AddSource: false,
			Level:     slog.LevelDebug,
		})), nil

	case EnvProd:
		return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
			AddSource: false,
			Level:     slog.LevelError,
		})), nil

	default:
		return nil, ErrInvalidLogLevel
	}
}
