package cn.hztech.sdk.api;

import cn.hztech.sdk.client.SdkHttpClient;
import cn.hztech.sdk.model.PageResult;
import cn.hztech.sdk.model.entity.Menu;
import cn.hztech.sdk.model.request.MenuCreateRequest;
import cn.hztech.sdk.model.request.MenuDeleteRequest;
import cn.hztech.sdk.model.request.MenuQueryRequest;
import cn.hztech.sdk.model.request.MenuUpdateRequest;
import cn.hztech.sdk.model.response.MenuCreateResponse;
import cn.hztech.sdk.model.response.MenuDeleteResponse;
import cn.hztech.sdk.model.response.MenuUpdateResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 菜单管理API客户端
 *
 * 提供以下接口：
 * - query: 查询菜单数据
 */
@Slf4j
public class MenuApiClient {

    private static final String API_PREFIX = "/hztech-application/menu";

    private final SdkHttpClient httpClient;

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

    /**
     * 查询菜单列表（便捷方法）
     */
    public PageResult<Menu> queryList(MenuQueryRequest request) {
        request.setQueryType("list");
        return (PageResult<Menu>) query(request);
    }

    /**
     * 查询菜单树（便捷方法）
     */
    public List<Menu> queryTree(MenuQueryRequest request) {
        request.setQueryType("tree");
        return (List<Menu>) query(request);
    }
    /**
     * 查询菜单数据
     *
     * @param request 查询请求
     * @return 菜单列表
     */
    public Object query(MenuQueryRequest request) {
        log.info("查询菜单数据, request: {}", request);
        if ("tree".equals(request.getQueryType())) {
            // 返回树形结构
            return httpClient.post(API_PREFIX + "/query", request, new TypeReference<List<Menu>>() {});
        } else {
            // 返回分页列表（默认）
            return httpClient.post(API_PREFIX + "/query", request, new TypeReference<PageResult<Menu>>() {});
        }

    }
    /**
     * 查询菜单数据
     *
     * @param request 查询请求
     * @return 菜单列表
     */
    public MenuCreateResponse create(MenuCreateRequest request) {
        log.info("创建菜单数据, request: {}", request);
        return httpClient.post(API_PREFIX + "/create", request, new TypeReference<MenuCreateResponse>() {});
    }
    /**
     * 查询菜单数据
     *
     * @param request 查询请求
     * @return 菜单列表
     */
    public MenuUpdateResponse update(MenuUpdateRequest request) {
        log.info("查询菜单数据, request: {}", request);
        return httpClient.post(API_PREFIX + "/update", request, new TypeReference<MenuUpdateResponse>() {});
    }
    /**
     * 查询菜单数据
     *
     * @param request 查询请求
     * @return 菜单列表
     */
    public MenuDeleteResponse delete(MenuDeleteRequest request) {
        log.info("查询菜单数据, request: {}", request);
        return httpClient.post(API_PREFIX + "/delete", request, new TypeReference<MenuDeleteResponse>() {});
    }


}
