package leaderelection

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

	"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/config"
)

// ProvideLeaderElection provides a LeaderElection instance for Wire DI.
func ProvideLeaderElection(
	cfg *config.Config,
	redisClient *redis.Client,
	logger *zap.Logger,
) (LeaderElection, error) {
	// Create configuration from app config
	// InstanceID and Hostname are auto-generated by NewRedisLeaderElection
	leConfig := &Config{
		RedisKeyName:      "maplepress:leader:lock",
		RedisInfoKeyName:  "maplepress:leader:info",
		LockTTL:           cfg.LeaderElection.LockTTL,
		HeartbeatInterval: cfg.LeaderElection.HeartbeatInterval,
		RetryInterval:     cfg.LeaderElection.RetryInterval,
		InstanceID:        "", // Auto-generated from hostname + random suffix
		Hostname:          "", // Auto-detected from os.Hostname()
	}

	// redis.Client implements redis.UniversalClient interface
	return NewRedisLeaderElection(leConfig, redisClient, logger)
}
