package com.cyksj.web.controller.recharge;

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.request.GroupsRelationRechargeReq;
import com.cyksj.server.recharge.GptProxyRechargeService;
import com.cyksj.server.recharge.dto.TaskResult;
import com.cyksj.server.recharge.dto.TokenParseResult;
import com.cyksj.web.controller.recharge.req.ParseTokenRequest;
import com.cyksj.web.util.StpUserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * server/GPT代理充值控制器
 */
@RestController
@RequestMapping("/gpt-recharge")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class GptRechargeController {

    private final GptProxyRechargeService gptProxyRechargeService;

    /**
     * 验证凭证
     */
    @PostMapping("/parse-token")
    public Result<TokenParseResult> parseToken(@RequestBody ParseTokenRequest request) {
        TokenParseResult tokenParseResult = gptProxyRechargeService.parseToken(request.getAccessToken());
        return GatewayResponse.SUCCESS.newBuilder().toResult(tokenParseResult);
    }

    /**
     * 用户确认充值
     */
    @PostMapping("/confirm")
    public Result<String> confirmRecharge(@RequestBody @Validated GroupsRelationRechargeReq req) {
        long userId = StpUserUtil.getLoginIdAsLong();
        req.setUserId(userId);
        String taskId = gptProxyRechargeService.confirmRecharge(req);
        return GatewayResponse.SUCCESS.newBuilder().toResult(taskId);
    }


    /**
     * 查询任务状态
     */
    @GetMapping("/task-status/{taskId}")
    public Result<TaskResult> getTaskStatus(@PathVariable String taskId) {
        long userId = StpUserUtil.getLoginIdAsLong();
        return GatewayResponse.SUCCESS.newBuilder().toResult(gptProxyRechargeService.getTaskResult(taskId));
    }
}