package cn.hztech.sdk.api;

import cn.hztech.sdk.client.SdkHttpClient;
import cn.hztech.sdk.model.PageResult;
import cn.hztech.sdk.model.entity.ApiMetadata;
import cn.hztech.sdk.model.request.ApiCreateRequest;
import cn.hztech.sdk.model.request.ApiDetailRequest;
import cn.hztech.sdk.model.request.ApiListRequest;
import cn.hztech.sdk.model.request.ApiUpdateRequest;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;

/**
 * 接口管理API客户端
 *
 * 提供以下接口：
 * - create: 创建接口元数据
 * - update: 更新接口元数据
 * - delete: 删除接口元数据
 * - getDetail: 查询接口详情
 * - getList: 接口列表查询
 */
@Slf4j
public class ApiManagementApiClient {

    private static final String API_PREFIX = "/hztech-interface/api";

    private final SdkHttpClient httpClient;

    public ApiManagementApiClient(SdkHttpClient httpClient) {
        this.httpClient = httpClient;
    }

    /**
     * 创建接口元数据
     *
     * @param request 创建请求
     * @return API元数据
     */
    public ApiMetadata create(ApiCreateRequest request) {
        log.info("创建接口元数据, request: {}", request);
        return httpClient.post(API_PREFIX + "/create", request, ApiMetadata.class);
    }

    /**
     * 更新接口元数据
     *
     * @param request 更新请求
     * @return API元数据
     */
    public ApiMetadata update(ApiUpdateRequest request) {
        log.info("更新接口元数据, request: {}", request);
        return httpClient.post(API_PREFIX + "/update", request, ApiMetadata.class);
    }

    /**
     * 删除接口元数据
     *
     * @param apiId API ID
     */
    public void delete(String apiId) {
        log.info("删除接口元数据, apiId: {}", apiId);
        ApiDetailRequest request = new ApiDetailRequest();
        request.setId(apiId);
        httpClient.post(API_PREFIX + "/delete", request, Void.class);
    }

    /**
     * 查询接口详情
     *
     * @param apiId API ID
     * @return API元数据
     */
    public ApiMetadata getDetail(String apiId) {
        log.info("查询接口详情, apiId: {}", apiId);
        ApiDetailRequest apiDetailRequest = new ApiDetailRequest();
        apiDetailRequest.setId(apiId);
        return httpClient.post(API_PREFIX + "/detail", apiDetailRequest, ApiMetadata.class);
    }

    /**
     * 接口列表查询
     *
     * @param request 查询请求
     * @return 分页结果
     */
    public PageResult<ApiMetadata> getList(ApiListRequest request) {
        log.info("接口列表查询, request: {}", request);
        return httpClient.post(API_PREFIX + "/list", request, new TypeReference<PageResult<ApiMetadata>>() {});
    }
}
