package cachingsvc

import (
	"context"
	"encoding/json"

	cachingdomain "legalgo-BE-go/internal/domain/caching"
	"legalgo-BE-go/internal/utilities/response"

	"github.com/sirupsen/logrus"
)

func (i *impl) Set(ctx context.Context, spec cachingdomain.CacheSpec) error {
	redisClient := i.redisClient

	if redisClient == nil {
		logrus.WithContext(ctx).Errorf("redis not found")

		return response.ErrRedisConnNotFound
	}

	dataBytes, err := json.Marshal(spec.Data)
	if err != nil {
		return response.ErrMarshal
	}

	if err := redisClient.Set(ctx, spec.Key, string(dataBytes), spec.TTL); err != nil {
		return response.ErrRedisSet
	}

	return nil
}
