package cn.hztech.sdk.api;

import cn.hztech.sdk.client.SdkHttpClient;
import cn.hztech.sdk.model.PageResult;
import cn.hztech.sdk.model.entity.Department;
import cn.hztech.sdk.model.request.DepartmentDetailRequest;
import cn.hztech.sdk.model.request.DepartmentListRequest;
import cn.hztech.sdk.model.request.DepartmentSearchRequest;
import cn.hztech.sdk.model.request.DepartmentTreeRequest;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 部门管理API客户端
 *
 * 提供以下接口：
 * - getDetail: 查询部门详情
 * - getList: 部门列表查询
 * - getTree: 部门树查询
 * - search: 部门搜索
 */
@Slf4j
public class DepartmentApiClient {

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

    private final SdkHttpClient httpClient;

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

    /**
     * 查询部门详情
     *
     * @param deptId 部门ID
     * @return 部门信息
     */
    public Department getDetail(String deptId) {
        log.info("查询部门详情, deptId: {}", deptId);
        DepartmentDetailRequest request = new DepartmentDetailRequest();
        request.setId(deptId);
        return httpClient.post(API_PREFIX + "/detail", request, Department.class);
    }

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

    /**
     * 查询部门树
     *
     * @return 部门树
     */
    public List<Department> getTree(DepartmentTreeRequest request) {
        log.info("查询部门树");
        return httpClient.post(API_PREFIX + "/tree", request, new TypeReference<List<Department>>() {});
    }

    /**
     * 搜索部门
     *
     * @param request 搜索请求
     * @return 分页结果
     */
    public PageResult<Department> search(DepartmentSearchRequest request) {
        log.info("搜索部门, request: {}", request);
        return httpClient.post(API_PREFIX + "/search", request, new TypeReference<PageResult<Department>>() {});
    }
}
