import { http } from "@/utils/http";

export interface WxUserDTO {
  /** 主键ID */
  wxUserId?: number;
  /** openid */
  openid?: string;
  /** 汇邦云用户ID */
  ab98UserId?: number;
  /** 企业用户ID */
  qyUserId?: number;
  /** 昵称 */
  nickName?: string;
  /** 手机号码 */
  tel?: string;
  /** 用户余额（单位：分） */
  wxBalance?: number;
  /** 创建时间 */
  createTime?: string;
  /** 更新时间 */
  updateTime?: string;
  /** 备注 */
  remark?: string;
}

export interface SearchWxUserQuery extends BasePageQuery {
  /** 微信用户ID */
  wxUserId?: number;
  /** openid */
  openid?: string;
  /** 昵称 */
  nickName?: string;
  /** 手机号码 */
  tel?: string;
  /** 汇邦云用户ID */
  ab98UserId?: number;
  /** 企业用户ID */
  qyUserId?: number;
  /** 最小余额（分） */
  minBalance?: number;
  /** 最大余额（分） */
  maxBalance?: number;
}

export interface AddWxUserCommand {
  /** openid */
  openid?: string;
  /** 汇邦云用户ID */
  ab98UserId?: number;
  /** 企业用户ID */
  qyUserId?: number;
  /** 昵称 */
  nickName?: string;
  /** 手机号码 */
  tel?: string;
  /** 余额（分） */
  wxBalance?: string;
  /** 备注 */
  remark?: string;
}

export interface UpdateWxUserCommand extends AddWxUserCommand {
  /** 主键ID */
  wxUserId: number;
}

/**
 * 获取微信用户列表
 */
export const getWxUserListApi = (params: SearchWxUserQuery) => {
  return http.request<ResponseData<PageDTO<WxUserDTO>>>("get", "/wx/users", { params });
};

/**
 * 获取微信用户详情
 */
export const getWxUserDetailApi = (id: number) => {
  return http.request<ResponseData<WxUserDTO>>("get", `/wx/users/detail/${id}`);
};

/**
 * 根据openid获取微信用户详情
 */
export const getWxUserDetailByOpenidApi = (openid: string) => {
  return http.request<ResponseData<WxUserDTO>>("get", "/wx/users/detail/by-openid", {
    params: { openid }
  });
};

/**
 * 根据openid获取或创建微信用户
 */
export const getOrCreateWxUserByOpenidApi = (openid: string) => {
  return http.request<ResponseData<WxUserDTO>>("get", "/wx/users/get-or-create/by-openid", {
    params: { openid }
  });
};

/**
 * 新增微信用户
 */
export const addWxUserApi = (data: AddWxUserCommand) => {
  return http.request<ResponseData<void>>("post", "/wx/users", { data });
};

/**
 * 修改微信用户
 */
export const updateWxUserApi = (id: number, data: UpdateWxUserCommand) => {
  return http.request<ResponseData<void>>("put", `/wx/users/${id}`, { data });
};

/**
 * 删除微信用户
 */
export const deleteWxUserApi = (id: number) => {
  return http.request<ResponseData<void>>("delete", `/wx/users/${id}`);
};

/**
 * 批量删除微信用户
 */
export const deleteWxUserBatchApi = (ids: number[]) => {
  return http.request<ResponseData<void>>("delete", `/wx/users/batch/${ids.join(',')}`);
};

/**
 * 增加微信用户余额
 */
export const increaseWxUserBalanceApi = (id: number, amount: number) => {
  return http.request<ResponseData<void>>("post", `/wx/users/${id}/increase-balance`, {
    params: { amount }
  });
};

/**
 * 减少微信用户余额
 */
export const decreaseWxUserBalanceApi = (id: number, amount: number) => {
  return http.request<ResponseData<void>>("post", `/wx/users/${id}/decrease-balance`, {
    params: { amount }
  });
};

/**
 * 设置微信用户余额
 */
export const setWxUserBalanceApi = (id: number, balance: number) => {
  return http.request<ResponseData<void>>("post", `/wx/users/${id}/set-balance`, {
    params: { balance }
  });
};