package mongo

import (
	"reflect"

	"github.com/shopspring/decimal"
	"github.com/zeromicro/go-zero/core/stores/mon"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

// SetCustomDecimalType registers decimal.Decimal <-> BSON Decimal128 conversion.
func SetCustomDecimalType() mon.Option {
	return mon.WithTypeCodec(mon.TypeCodec{
		ValueType: reflect.TypeOf(decimal.Decimal{}),
		Encoder:   &MgoDecimal{},
		Decoder:   &MgoDecimal{},
	})
}

// InitMongoOptions applies pool / compressor settings from Conf.
func InitMongoOptions(cfg Conf) mon.Option {
	return func(opts *options.ClientOptions) {
		if cfg.MaxPoolSize > 0 {
			opts.SetMaxPoolSize(cfg.MaxPoolSize)
		}
		if cfg.MinPoolSize > 0 {
			opts.SetMinPoolSize(cfg.MinPoolSize)
		}
		if cfg.MaxConnIdleTime > 0 {
			opts.SetMaxConnIdleTime(cfg.MaxConnIdleTime)
		}
		opts.SetCompressors(defaultCompressors(cfg))
	}
}
