// Package log defines the core logging functionality for Abra.
package log

import (
	"os"
	"strings"

	"github.com/charmbracelet/lipgloss"
	charmLog "github.com/charmbracelet/log"
)

// Logger is the central logging interface.
var Logger = charmLog.NewWithOptions(os.Stdout, charmLog.Options{
	ReportCaller:    false,
	ReportTimestamp: false,
})

var Fatal = Logger.Fatal
var Fatalf = Logger.Fatalf

var Debug = Logger.Debug
var Debugf = Logger.Debugf

var Info = Logger.Info
var Infof = Logger.Infof

var Warn = Logger.Warn
var Warnf = Logger.Warnf

var Error = Logger.Error
var Errorf = Logger.Errorf

var SetLevel = Logger.SetLevel
var DebugLevel = charmLog.DebugLevel
var SetOutput = charmLog.SetOutput
var SetReportCaller = charmLog.SetReportCaller

func Styles() *charmLog.Styles {
	styles := charmLog.DefaultStyles()

	styles.Levels = map[charmLog.Level]lipgloss.Style{
		charmLog.DebugLevel: lipgloss.NewStyle().
			SetString(strings.ToUpper(DebugLevel.String())).
			Bold(true).
			Padding(0, 1, 0, 1).
			Background(lipgloss.Color("63")).
			Foreground(lipgloss.Color("15")),
		charmLog.InfoLevel: lipgloss.NewStyle().
			SetString(strings.ToUpper(charmLog.InfoLevel.String())).
			Bold(true).
			Padding(0, 1, 0, 1).
			Background(lipgloss.Color("86")).
			Foreground(lipgloss.Color("16")),
		charmLog.WarnLevel: lipgloss.NewStyle().
			SetString(strings.ToUpper(charmLog.WarnLevel.String())).
			Bold(true).
			Padding(0, 1, 0, 1).
			Background(lipgloss.Color("192")).
			Foreground(lipgloss.Color("16")),
		charmLog.ErrorLevel: lipgloss.NewStyle().
			SetString(strings.ToUpper(charmLog.ErrorLevel.String())).
			Bold(true).
			Padding(0, 1, 0, 1).
			Background(lipgloss.Color("204")).
			Foreground(lipgloss.Color("15")),
		charmLog.FatalLevel: lipgloss.NewStyle().
			SetString(strings.ToUpper(charmLog.FatalLevel.String())).
			Bold(true).
			Padding(0, 1, 0, 1).
			Background(lipgloss.Color("134")).
			Foreground(lipgloss.Color("15")),
	}

	return styles
}
