package cache

import (
	"fmt"
	"time"

	"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
	"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
	oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
	"github.com/bluele/gcache"
	"go.uber.org/zap"
)

type (
	// AccessBoxCache stores an access box by its address.
	AccessBoxCache struct {
		logger *zap.Logger
		cache  gcache.Cache
	}

	// Config stores expiration params for cache.
	Config struct {
		Size     int
		Lifetime time.Duration
		Logger   *zap.Logger
	}

	AccessBoxCacheValue struct {
		Box        *accessbox.Box
		Attributes []object.Attribute
		PutTime    time.Time
	}
)

const (
	// DefaultAccessBoxCacheSize is a default maximum number of entries in cache.
	DefaultAccessBoxCacheSize = 100
	// DefaultAccessBoxCacheLifetime is a default lifetime of entries in cache.
	DefaultAccessBoxCacheLifetime = 10 * time.Minute
)

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

// NewAccessBoxCache creates an object of AccessBoxCache.
func NewAccessBoxCache(config *Config) *AccessBoxCache {
	gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()

	return &AccessBoxCache{cache: gc, logger: config.Logger}
}

// Get returns a cached accessbox.
func (o *AccessBoxCache) Get(address oid.Address) *AccessBoxCacheValue {
	entry, err := o.cache.Get(address)
	if err != nil {
		return nil
	}

	result, ok := entry.(*AccessBoxCacheValue)
	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 stores an accessbox to cache.
func (o *AccessBoxCache) Put(address oid.Address, box *accessbox.Box, attrs []object.Attribute) error {
	val := &AccessBoxCacheValue{
		Box:        box,
		Attributes: attrs,
		PutTime:    time.Now(),
	}
	return o.cache.Set(address, val)
}

// Delete removes an accessbox from cache.
func (o *AccessBoxCache) Delete(address oid.Address) {
	o.cache.Remove(address)
}