package leaderelection

import (
	"github.com/redis/go-redis/v9"
	"go.uber.org/zap"

	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/pkg/distributedmutex"
)

// ProvideLeaderElection provides a LeaderElection instance for Wire DI.
func ProvideLeaderElection(
	cfg *config.Config,
	mutex distributedmutex.Adapter,
	redisClient redis.UniversalClient,
	logger *zap.Logger,
) (LeaderElection, error) {
	// Create configuration from app config
	leConfig := &Config{
		RedisKeyName:      "maplefile:leader:lock",
		RedisInfoKeyName:  "maplefile:leader:info",
		LockTTL:           cfg.LeaderElection.LockTTL,
		HeartbeatInterval: cfg.LeaderElection.HeartbeatInterval,
		RetryInterval:     cfg.LeaderElection.RetryInterval,
		InstanceID:        cfg.LeaderElection.InstanceID,
		Hostname:          cfg.LeaderElection.Hostname,
	}

	return NewMutexLeaderElection(leConfig, mutex, redisClient, logger)
}
