package com.kamco.cd.kamcoback.auth;

import java.time.Duration;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RefreshTokenService {

  private final StringRedisTemplate redisTemplate;
  private static final String PREFIX = "RT:";

  public void save(String username, String refreshToken, long ttlMillis) {
    ValueOperations<String, String> ops = redisTemplate.opsForValue();
    ops.set(PREFIX + username, refreshToken, Duration.ofMillis(ttlMillis));
  }

  public boolean validate(String username, String refreshToken) {
    String stored = redisTemplate.opsForValue().get(PREFIX + username);
    return stored != null && stored.equals(refreshToken);
  }

  public void delete(String username) {
    redisTemplate.delete(PREFIX + username);
  }
}
