package mongoLogger

import (
	"Yimaru-Backend/internal/config"
	"fmt"
	"os"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func InitLogger(cfg *config.Config) (*zap.Logger, error) {
	mongoCore, err := NewMongoCore(
		os.Getenv("MONGODB_URL"),
		"logdb",
		"applogs",
		zapcore.InfoLevel,
		cfg,
	)
	if err != nil {
		return nil, fmt.Errorf("failed to create MongoDB core: %w", err)
	}

	consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
	consoleCore := zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), zapcore.DebugLevel)

	combinedCore := zapcore.NewTee(mongoCore, consoleCore)

	logger := zap.New(combinedCore, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))

	return logger, nil
}
