package svc

import (
	errs "backend/pkg/library/errors"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type logger struct {
	l logx.Logger
}

func (lgr *logger) WithCallerSkip(skip int) errs.Logger {
	return &logger{
		l: logx.WithContext(context.Background()).WithCallerSkip(skip),
	}
}

func (lgr *logger) WithFields(fields ...errs.LogField) errs.Logger {
	return &logger{
		l: logx.WithContext(context.Background()).WithFields(fTof(fields)...),
	}
}

func (lgr *logger) Error(msg string) {
	lgr.l.Error(msg)
}

func (lgr *logger) Info(msg string) {
	lgr.l.Info(msg)
}

func (lgr *logger) Warn(msg string) {
	lgr.l.Error(msg)
}

func MustLogger(log logx.Logger) errs.Logger {
	return &logger{
		l: log,
	}
}

func fTof(field []errs.LogField) []logx.LogField {
	f := make([]logx.LogField, 0, len(field)+1)
	for _, v := range field {
		f = append(f, logx.LogField{
			Key:   v.Key,
			Value: v.Val,
		})
	}

	return f
}
