package com.tashow.cloud.system.controller.admin.oauth2;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.system.controller.admin.oauth2.vo.token.OAuth2AccessTokenPageReqVO;
import com.tashow.cloud.system.controller.admin.oauth2.vo.token.OAuth2AccessTokenRespVO;
import com.tashow.cloud.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
import com.tashow.cloud.system.service.auth.AdminAuthService;
import com.tashow.cloud.system.service.oauth2.OAuth2TokenService;
import com.tashow.cloud.systemapi.enums.logger.LoginLogTypeEnum;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.tashow.cloud.common.pojo.CommonResult.success;

/**
 * 管理后台 - OAuth2.0 令牌
 */
@RestController
@RequestMapping("/system/oauth2-token")
public class OAuth2TokenController {

    @Resource
    private OAuth2TokenService oauth2TokenService;
    @Resource
    private AdminAuthService authService;

    /**
     * 获得访问令牌分页", description = "只返回有效期内的
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:oauth2-token:page')")
    public CommonResult<PageResult<OAuth2AccessTokenRespVO>> getAccessTokenPage(
            @Valid OAuth2AccessTokenPageReqVO reqVO) {
        PageResult<OAuth2AccessTokenDO> pageResult = oauth2TokenService.getAccessTokenPage(reqVO);
        return success(BeanUtils.toBean(pageResult, OAuth2AccessTokenRespVO.class));
    }

    /**
     * 删除访问令牌
     */
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPermission('system:oauth2-token:delete')")
    public CommonResult<Boolean> deleteAccessToken(@RequestParam("accessToken") String accessToken) {
        authService.logout(accessToken, LoginLogTypeEnum.LOGOUT_DELETE.getType());
        return success(true);
    }
}
