package com.red.circle.auth.endpoint;

import com.red.circle.auth.common.ProcessToken;
import com.red.circle.auth.inner.endpoint.api.AuthClientApi;
import com.red.circle.auth.storage.RedCircleCredentialService;
import com.red.circle.framework.core.security.UserCredential;
import com.red.circle.framework.dto.ResultResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 令牌端点.
 *
 * @author pengliang on 2023/8/16
 */
@Validated
@RestController
@RequestMapping(value = AuthClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class AuthClientEndpoint implements AuthClientApi {

  private final ProcessToken processToken;
  private final RedCircleCredentialService redCircleCredentialService;

  @Override
  public ResultResponse<Void> removeToken(Long userId) {
    redCircleCredentialService.removeByUserId(userId);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<UserCredential> getUserCredential(Long userId) {
    return ResultResponse.success(redCircleCredentialService.getByUserId(userId));
  }

  @Override
  public ResultResponse<UserCredential> getUserCredential(String token) {
    return ResultResponse.success(redCircleCredentialService.getByUserToken(token));
  }

  @Override
  public ResultResponse<String> createIfAbsentUserCredentialToken(Long userId) {
    return ResultResponse.success(processToken.createIfAbsentUserCredentialToken(userId));
  }

}
