package com.kingdee.eas.custom.integration.dingtalk.controller;

import com.kingdee.eas.custom.integration.base.annotation.ApiDoc;
import com.kingdee.eas.custom.integration.base.annotation.SkipAuth;
import com.kingdee.eas.custom.integration.base.annotation.SkipCallLog;
import com.kingdee.eas.custom.integration.base.controller.AbstractAuthController;
import com.kingdee.eas.custom.integration.base.exception.CommonErrorCodes;
import com.kingdee.eas.custom.integration.base.exception.IntegrationException;
import com.kingdee.eas.custom.integration.base.service.IntegrationConfigService;
import com.kingdee.eas.custom.integration.base.service.OAuthService;
import com.kingdee.eas.custom.integration.base.service.StateValidator;
import com.kingdee.eas.custom.integration.base.service.detection.PlatformDetectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 钉钉单点认证控制器。
 *
 * <p>认证入口：{@code GET /auth/dingtalk?state=xxx}</p>
 * <p>回调地址：{@code GET /auth/callback/dingtalk/{appType}}（在钉钉开放平台注册）</p>
 * <p>对接新应用时直接复用此类，无需修改。</p>
 *
 * <p>注解说明：</p>
 * <ul>
 *   <li>@SkipAuth — 单点认证流程无需密钥验证</li>
 *   <li>@SkipCallLog — 浏览器级 OAuth 跳转不记录调用日志</li>
 * </ul>
 */
@SkipAuth
@SkipCallLog
@Controller
@RequestMapping("/auth")
public class DingtalkAuthController extends AbstractAuthController {

    public DingtalkAuthController(IntegrationConfigService appConfigService,
                                  OAuthService oAuthService,
                                  StateValidator stateValidator,
                                  PlatformDetectionService detectionService) {
        super(appConfigService, oAuthService, stateValidator, detectionService);
    }

    @Override
    protected String platform() {
        return "dingtalk";
    }

    /**
     * 钉钉 OAuth 认证入口，由 sHR 前端页面跳转至此。
     * state 参数包含 redirect 和 appType，由父类 doAuth 解析处理。
     */
    @GetMapping("/dingtalk")
    @ApiDoc(name = "钉钉认证入口", desc = "sHR 前端页面跳转至此")
    public void auth(@RequestParam(name = "state", required = false) String state,
                     HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(state)) {
            throw new IntegrationException(CommonErrorCodes.PARAM_MISSING, "Invalid state");
        }
        doAuth(state, response);
    }

    /**
     * 钉钉 OAuth 回调接口，钉钉开放平台授权后回调此地址。
     * 由父类 doCallback 完成 code 换 token、用户查询、最终重定向。
     */
    @GetMapping("/callback/dingtalk/{appType}")
    @ApiDoc(name = "钉钉回调接口", desc = "钉钉开放平台授权后回调此地址")
    public void callback(String code, String state,
                         @PathVariable String appType,
                         HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        doCallback(code, state, request, response);
    }
}
