package com.malk.benteler.controller;

import com.malk.benteler.dto.EiamTestResult;
import com.malk.benteler.dto.FormInstanceRequest;
import com.malk.benteler.service.BentelerYidaTestService;
import com.malk.server.common.McR;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 宜搭连接器测试接口。
 */
@RestController
@RequestMapping("/benteler/eiam/test")
public class EiamTestController {

    private final BentelerYidaTestService yidaTestService;

    public EiamTestController(BentelerYidaTestService yidaTestService) {
        this.yidaTestService = yidaTestService;
    }

    /**
     * 读取当前宜搭实例的最终处理结果，并在接口执行后回写为成功。
     *
     * @param request JSON 请求体中的宜搭表单实例 ID
     * @param formInstanceId Query 参数中的宜搭表单实例 ID（兼容宜搭连接器）
     * @return 字段回写前后的值
     */
    @PostMapping("/mark-result-success")
    public McR<EiamTestResult> markResultSuccess(
            @RequestBody(required = false) FormInstanceRequest request,
            @RequestParam(value = "formInstanceId", required = false) String formInstanceId) {
        String bodyFormInstanceId = request == null ? null : request.getFormInstanceId();
        return McR.success(yidaTestService.markResultSuccess(
                StringUtils.defaultIfBlank(bodyFormInstanceId, formInstanceId)));
    }
}
