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.entity.Role;
import cn.hztech.sdk.model.entity.User;
import cn.hztech.sdk.model.request.UserDetailRequest;
import cn.hztech.sdk.model.request.UserListRequest;
import cn.hztech.sdk.model.request.UserSearchRequest;
import cn.hztech.sdk.model.response.ApiResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 人员管理API客户端
 *
 * 提供以下接口：
 * - getDetail: 查询人员详情
 * - getList: 人员列表查询
 * - search: 人员搜索
 * - getRoles: 查询人员角色
 * - getDepartment: 查询人员部门
 */
@Slf4j
public class UserApiClient {

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

    private final SdkHttpClient httpClient;

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

    /**
     * 查询人员详情
     *
     * @param userId 用户ID
     * @return 用户信息
     */
    public User getDetail(String userId) {
        log.info("查询人员详情, userId: {}", userId);
        UserDetailRequest request = new UserDetailRequest();
        request.setId(userId);
        return httpClient.post(API_PREFIX + "/detail", request, User.class);
    }

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

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

    /**
     * 查询人员角色
     *
     * @param userId 用户ID
     * @return 角色信息
     */
    public Role getRoles(String userId) {
        log.info("查询人员角色, userId: {}", userId);
        UserDetailRequest request = new UserDetailRequest();
        request.setId(userId);
        return httpClient.post(API_PREFIX + "/roles", request, Role.class);
    }

    /**
     * 查询人员部门
     *
     * @param userId 用户ID
     * @return 部门信息
     */
    public Department getDepartment(String userId) {
        log.info("查询人员部门, userId: {}", userId);
        UserDetailRequest request = new UserDetailRequest();
        request.setId(userId);
        return httpClient.post(API_PREFIX + "/department", request, Department.class);
    }
}
