package logger

import (
	"io"
	"os"

	"github.com/sirupsen/logrus"
)

var log = logrus.New()

func Init(level string, filePath string) error {
	// Set log level
	lvl, err := logrus.ParseLevel(level)
	if err != nil {
		return err
	}
	log.SetLevel(lvl)

	// Configure output
	if filePath != "" {
		file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
		if err != nil {
			return err
		}
		log.SetOutput(io.MultiWriter(os.Stdout, file))
	}

	log.SetFormatter(&logrus.TextFormatter{
		FullTimestamp: true,
	})

	return nil
}

// Standard logging methods
func Debug(args ...interface{}) {
	log.Debug(args...)
}

func Info(args ...interface{}) {
	log.Info(args...)
}

func Error(args ...interface{}) {
	log.Error(args...)
}

func Warn(args ...interface{}) {
	log.Warn(args...)
}

// Structured logging methods
func WithField(key string, value interface{}) *logrus.Entry {
	return log.WithField(key, value)
}

func WithFields(fields logrus.Fields) *logrus.Entry {
	return log.WithFields(fields)
}

func WithError(err error) *logrus.Entry {
	return log.WithError(err)
}
