package storage

import (
	"sync"
	"time"
)

type ReplayCache struct {
	ttl  time.Duration
	mu   sync.Mutex
	data map[string]time.Time
}

func NewReplayCache(ttl time.Duration) *ReplayCache {
	return &ReplayCache{ttl: ttl, data: map[string]time.Time{}}
}

func (c *ReplayCache) CheckAndMark(hash string) (bool, error) {
	c.mu.Lock()
	defer c.mu.Unlock()
	now := time.Now()
	for k, ts := range c.data {
		if now.Sub(ts) > c.ttl {
			delete(c.data, k)
		}
	}
	if _, exists := c.data[hash]; exists {
		return true, nil
	}
	c.data[hash] = now
	return false, nil
}
