package storage

import (
	"context"
	"fmt"
	"log/slog"

	"github.com/redis/go-redis/v9"

	"github.com/denisovdennis/autohero/internal/config"
)

// NewRedis creates a Redis client and verifies the connection.
func NewRedis(ctx context.Context, cfg config.RedisConfig, logger *slog.Logger) (*redis.Client, error) {
	client := redis.NewClient(&redis.Options{
		Addr:     cfg.Addr,
		DB:       0,
		PoolSize: 20,
	})

	if err := client.Ping(ctx).Err(); err != nil {
		return nil, fmt.Errorf("ping redis: %w", err)
	}

	logger.Info("connected to Redis", "addr", cfg.Addr)

	return client, nil
}
