package redis

import (
	"context"

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

const KeyPrefix = "qycw:"

type Options struct {
	Addr     string
	Password string
	DB       int
}

type Client struct {
	*redis.Client
}

func Open(ctx context.Context, opts Options) (*Client, error) {
	client := redis.NewClient(&redis.Options{
		Addr:     opts.Addr,
		Password: opts.Password,
		DB:       opts.DB,
	})
	if err := client.Ping(ctx).Err(); err != nil {
		_ = client.Close()
		return nil, err
	}
	return &Client{Client: client}, nil
}

func Key(parts ...string) string {
	key := KeyPrefix
	for i, part := range parts {
		if i > 0 {
			key += ":"
		}
		key += part
	}
	return key
}
