package com.cool.modules.aiconfig.controller.admin;

import cn.hutool.json.JSONObject;
import com.cool.core.annotation.CoolRestController;
import com.cool.core.base.BaseController;
import com.cool.modules.aiconfig.entity.AiApiConfigEntity;
import com.cool.modules.aiconfig.service.AiApiConfigService;
import com.cool.core.request.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import static com.cool.modules.aiconfig.entity.table.AiApiConfigEntityTableDef.AI_API_CONFIG_ENTITY;

/**
 * AI模型接口配置管理
 */
@Tag(name = "AI接口配置", description = "AI模型接口配置管理")
@CoolRestController(api = {"add", "delete", "update", "page", "list", "info"})
public class AdminAiApiConfigController extends BaseController<AiApiConfigService, AiApiConfigEntity> {

    @Override
    protected void init(HttpServletRequest request, JSONObject requestParams) {
        setPageOption(createOp()
                .keyWordLikeFields(AI_API_CONFIG_ENTITY.API_URL)
                .fieldEq(AI_API_CONFIG_ENTITY.MODEL_ID)
        );
    }

    @Operation(summary = "测试接口连通性", description = "测试AI接口是否可以正常连接")
    @PostMapping("/testConnection")
    public R testConnection(@RequestBody JSONObject body) {
        Long id = body.getLong("id");
        boolean result = this.service.testConnection(id);

        if (result) {
            return R.ok("接口连接测试成功");
        } else {
            return R.error("接口连接测试失败，请检查配置");
        }
    }
}
