package logger

import (
	"fmt"
	"os"
	"time"

	"github.com/sirupsen/logrus"
)

type LogFormatter struct {
}

func (lf *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
	var err error
	timeStamp := time.Now().Format(time.RFC3339)
	levelString := entry.Level.String()
	labelString := ""
	for key, value := range entry.Data {
		labelString += fmt.Sprintf("<%s:%v>", key, value)
	}
	if labelString != "" {
		message := fmt.Sprintf("%s %s %s [%s]\n", timeStamp, levelString, labelString, entry.Message)
		return []byte(message), err
	}
	message := fmt.Sprintf("%s %s [%s]\n", timeStamp, levelString, entry.Message)
	return []byte(message), err
}

func init() {
	logrus.SetOutput(os.Stdout)
	logrus.SetFormatter(&LogFormatter{})
	logrus.SetLevel(logrus.DebugLevel)
}

type Logger struct {
	logrus *logrus.Entry
}

func NewLogger(label string) *Logger {
	return &Logger{
		logrus: logrus.WithField("object", label),
	}
}

func (log *Logger) Errorf(format string, args ...any) {
	log.logrus.Errorf(format, args...)
}

func (log *Logger) Debugf(format string, args ...any) {
	log.logrus.Debugf(format, args...)
}

func (log *Logger) Warningf(format string, args ...any) {
	log.logrus.Warningf(format, args...)
}

func (log *Logger) Infof(format string, args ...any) {
	log.logrus.Infof(format, args...)
}

func (log *Logger) Error(args ...any) {
	log.logrus.Error(args...)
}

func (log *Logger) Debug(args ...any) {
	log.logrus.Debug(args...)
}

func (log *Logger) Warning(args ...any) {
	log.logrus.Warning(args...)
}

func (log *Logger) Info(args ...any) {
	log.logrus.Info(args...)
}
