package net.prasenjit.identity.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.prasenjit.identity.entity.AccessToken;
import net.prasenjit.identity.entity.AuthorizationCode;
import net.prasenjit.identity.entity.RefreshToken;
import net.prasenjit.identity.model.OAuthToken;
import net.prasenjit.identity.repository.AccessTokenRepository;
import net.prasenjit.identity.repository.AuthorizationCodeRepository;
import net.prasenjit.identity.repository.RefreshTokenRepository;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Slf4j
@Component
@RequiredArgsConstructor
public class CodeFactory {

    private final AuthorizationCodeRepository authorizationCodeRepository;
    private final RefreshTokenRepository refreshTokenRepository;
    private final AccessTokenRepository accessTokenRepository;
    private final ObjectMapper objectMapper;

    public AuthorizationCode createAuthorizationCode(String clientId, String returnUrl, String scope, String userName, String state, Duration validity) {
        AuthorizationCode authorizationCode = new AuthorizationCode();
        authorizationCode.setClientId(clientId);
        LocalDateTime creationDate = LocalDateTime.now();
        authorizationCode.setCreationDate(creationDate);
        authorizationCode.setExpiryDate(creationDate.plus(validity));
        authorizationCode.setReturnUrl(returnUrl);
        authorizationCode.setScope(scope);
        authorizationCode.setUsername(userName);
        authorizationCode.setUsed(false);
        authorizationCode.setState(state);
        authorizationCode.setAuthorizationCode(RandomStringUtils.randomAlphanumeric(8));
        authorizationCodeRepository.saveAndFlush(authorizationCode);
        return authorizationCode;
    }

    public AccessToken createAccessToken(UserDetails user, String clientId, Duration duration, String scope) {
        AccessToken accessToken = new AccessToken();
        accessToken.setAssessToken(RandomStringUtils.randomAlphanumeric(24));
        accessToken.setUsername(user.getUsername());
        LocalDateTime creationDate = LocalDateTime.now();
        accessToken.setCreationDate(creationDate);
        accessToken.setExpiryDate(creationDate.plus(duration));
        accessToken.setUserProfile(user);
        accessToken.setClientId(clientId);
        accessToken.setScope(scope);
        accessTokenRepository.saveAndFlush(accessToken);
        return accessToken;
    }

    public RefreshToken createRefreshToken(String clientId, String userName, String scope, Duration duration) {
        RefreshToken refreshToken = new RefreshToken();
        refreshToken.setClientId(clientId);
        LocalDateTime creationDate = LocalDateTime.now();
        refreshToken.setCreationDate(creationDate);
        refreshToken.setExpiryDate(creationDate.plus(duration));
        refreshToken.setScope(scope);
        refreshToken.setUsername(userName);
        refreshToken.setRefreshToken(RandomStringUtils.randomAlphanumeric(24));
        refreshToken.setUsed(false);
        refreshTokenRepository.saveAndFlush(refreshToken);
        return refreshToken;
    }

    public OAuthToken createOAuthToken(AccessToken accessToken, RefreshToken refreshToken1) {
        OAuthToken oAuthToken = new OAuthToken();
        oAuthToken.setAccessToken(accessToken.getAssessToken());
        oAuthToken.setRefreshToken(refreshToken1.getRefreshToken());
        oAuthToken.setTokenType("Bearer");
        oAuthToken.setScope(accessToken.getScope());
        long expIn = ChronoUnit.SECONDS.between(LocalDateTime.now(), accessToken.getExpiryDate());
        oAuthToken.setExpiresIn(expIn);
        return oAuthToken;
    }
}

@@ -45,111 +45,265 @@
 ort 
-com.fasterxml.jackson.core.JsonProcessingException;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;
+java.time.Duration;%0Aimport java.time.LocalDateTime;%0Aimport java.time.temporal.ChronoUnit;%0A%0Aimport org.apache.commons.lang3.RandomStringUtils;%0Aimport org.springframework.security.core.userdetails.UserDetails;%0Aimport org.springframework.stereotype.Component;%0A
 %0Aimp
@@ -342,42 +342,8 @@
 or;%0A
-import lombok.extern.slf4j.Slf4j;%0A
 impo
@@ -746,273 +746,11 @@
 ry;%0A
-import org.apache.commons.lang3.RandomStringUtils;%0Aimport org.springframework.security.core.userdetails.UserDetails;%0Aimport org.springframework.stereotype.Component;%0A%0Aimport java.time.Duration;%0Aimport java.time.LocalDateTime;%0Aimport java.time.temporal.ChronoUnit;%0A%0A
+%0A//
 @Slf
@@ -1022,53 +1022,8 @@
 ory;
-%0A    private final ObjectMapper objectMapper;
 %0A%0A  

test fixed