package com.eactive.testmaster.login.service;

import com.eactive.testmaster.login.entity.Token;
import com.eactive.testmaster.login.repository.TokenRepository;
import java.time.LocalDateTime;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TokenService {

    @Autowired
    private TokenRepository tokenRepository;

    public Token createToken(String email) {
        String token = UUID.randomUUID().toString();
        LocalDateTime expiresAt = LocalDateTime.now().plusHours(24);
        Token tokenObj = new Token(token, email, expiresAt);
        return tokenRepository.save(tokenObj);
    }

    public Token findByToken(String token) {
        return tokenRepository.findById(token).orElse(null);
    }

    public void deleteToken(Token token) {
        tokenRepository.delete(token);
    }
}
