package cache

import (
	"fmt"
	"time"

	"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
	"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
	"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
	"github.com/bluele/gcache"
	"go.uber.org/zap"
)

// MorphPolicyCache provides lru cache for listing policies stored in policy contract.
type MorphPolicyCache struct {
	cache  gcache.Cache
	logger *zap.Logger
}

type MorphPolicyCacheKey struct {
	Target engine.Target
	Name   chain.Name
}

const (
	// DefaultMorphPolicyCacheSize is a default maximum number of entries in cache.
	DefaultMorphPolicyCacheSize = 1e4
	// DefaultMorphPolicyCacheLifetime is a default lifetime of entries in cache.
	DefaultMorphPolicyCacheLifetime = time.Minute
)

// DefaultMorphPolicyConfig returns new default cache expiration values.
func DefaultMorphPolicyConfig(logger *zap.Logger) *Config {
	return &Config{
		Size:     DefaultMorphPolicyCacheSize,
		Lifetime: DefaultMorphPolicyCacheLifetime,
		Logger:   logger,
	}
}

// NewMorphPolicyCache creates an object of MorphPolicyCache.
func NewMorphPolicyCache(config *Config) *MorphPolicyCache {
	gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()
	return &MorphPolicyCache{cache: gc, logger: config.Logger}
}

// Get returns a cached object. Returns nil if value is missing.
func (o *MorphPolicyCache) Get(key MorphPolicyCacheKey) []*chain.Chain {
	entry, err := o.cache.Get(key)
	if err != nil {
		return nil
	}

	result, ok := entry.([]*chain.Chain)
	if !ok {
		o.logger.Warn(logs.InvalidCacheEntryType, zap.String("actual", fmt.Sprintf("%T", entry)),
			zap.String("expected", fmt.Sprintf("%T", result)))
		return nil
	}

	return result
}

// Put puts an object to cache.
func (o *MorphPolicyCache) Put(key MorphPolicyCacheKey, list []*chain.Chain) error {
	return o.cache.Set(key, list)
}

// Delete deletes an object from cache.
func (o *MorphPolicyCache) Delete(key MorphPolicyCacheKey) bool {
	return o.cache.Remove(key)
}