package org.hzhub.workflow.wecom;

import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.hzhub.common.core.domain.R;
import org.hzhub.common.log.annotation.Log;
import org.hzhub.common.log.enums.BusinessType;
import org.hzhub.workflow.domain.bo.WecomTenantConfigBo;
import org.hzhub.workflow.domain.vo.WecomTenantConfigVo;
import org.hzhub.workflow.service.IWecomTenantConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 企业微信租户配置控制器
 *
 * @author hzhub
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wecom/tenant-config")
public class WecomTenantConfigController {

    private final IWecomTenantConfigService wecomTenantConfigService;

    /**
     * 获取当前租户的企业微信配置
     */
    @GetMapping
    public R<WecomTenantConfigVo> getConfig() {
        return R.ok(wecomTenantConfigService.getByCurrentTenant());
    }

    /**
     * 保存/更新企业微信配置
     */
    @Log(title = "企业微信配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> saveConfig(@Validated @RequestBody WecomTenantConfigBo bo) {
        wecomTenantConfigService.saveOrUpdate(bo);
        return R.ok("保存成功");
    }

    /**
     * 测试企业微信连接
     */
    @PostMapping("/test")
    public R<String> testConnection(
        @NotBlank(message = "企业ID不能为空") @RequestParam String corpid,
        @NotBlank(message = "应用密钥不能为空") @RequestParam String corpsecret) {
        String result = wecomTenantConfigService.testConnection(corpid, corpsecret);
        return R.ok(result);
    }
}
