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

/**
 * 会员标签关联数据传输对象
 */
export interface MembershipTagMemberDTO {
  /** 主键，自动递增 */
  id?: number;
  /** 企业微信ID */
  corpid?: string;
  /** 标签ID */
  tagId?: number;
  /** 汇邦云用户ID */
  ab98UserId?: number;
  /** 微信用户ID */
  wxUserId?: number;
  /** 创建者ID */
  creatorId?: number;
  /** 创建时间 */
  createTime?: string;
  /** 更新者ID */
  updaterId?: number;
  /** 更新时间 */
  updateTime?: string;
  /** 标签名称（可选，通过关联查询或缓存获取） */
  tagName?: string;
}

/**
 * 会员标签关联查询参数
 */
export interface SearchMembershipTagMemberQuery extends BasePageQuery {
  /** 企业微信ID */
  corpid?: string;
  /** 标签ID */
  tagId?: number;
  /** 汇邦云用户ID */
  ab98UserId?: number;
  /** 微信用户ID */
  wxUserId?: number;
}

/**
 * 添加会员标签关联命令
 */
export interface AddMembershipTagMemberCommand {
  /** 企业微信ID */
  corpid?: string;
  /** 标签ID */
  tagId: number;
  /** 汇邦云用户ID */
  ab98UserId?: number;
  /** 微信用户ID */
  wxUserId?: number;
}

/**
 * 更新会员标签关联命令
 */
export interface UpdateMembershipTagMemberCommand extends AddMembershipTagMemberCommand {
  /** 主键，自动递增 */
  id: number;
}

/**
 * 获取会员标签关联列表
 */
export const getMembershipTagMemberListApi = (params: SearchMembershipTagMemberQuery) => {
  return http.request<ResponseData<PageDTO<MembershipTagMemberDTO>>>("get", "/membership/tagMembers", { params });
};

/**
 * 获取标签关联详情
 */
export const getMembershipTagMemberDetailApi = (id: number) => {
  return http.request<ResponseData<MembershipTagMemberDTO>>("get", `/membership/tagMembers/${id}`);
};

/**
 * 添加会员标签关联
 */
export const addMembershipTagMemberApi = (data: AddMembershipTagMemberCommand) => {
  return http.request<ResponseData<void>>("post", "/membership/tagMembers", { data });
};

/**
 * 更新会员标签关联
 */
export const updateMembershipTagMemberApi = (id: number, data: UpdateMembershipTagMemberCommand) => {
  return http.request<ResponseData<void>>("put", `/membership/tagMembers/${id}`, { data });
};

/**
 * 删除会员标签关联
 */
export const deleteMembershipTagMemberApi = (ids: number[]) => {
  return http.request<ResponseData<void>>("delete", `/membership/tagMembers/${ids.join(',')}`);
};

/**
 * 根据标签ID查询关联用户
 */
export const getMembershipTagMemberByTagIdApi = (tagId: number) => {
  return http.request<ResponseData<MembershipTagMemberDTO[]>>("get", `/membership/tagMembers/byTag/${tagId}`);
};

/**
 * 根据汇邦云用户ID查询标签
 */
export const getMembershipTagMemberByAb98UserIdApi = (ab98UserId: number) => {
  return http.request<ResponseData<MembershipTagMemberDTO[]>>("get", `/membership/tagMembers/byAb98User/${ab98UserId}`);
};

/**
 * 根据微信用户ID查询标签
 */
export const getMembershipTagMemberByWxUserIdApi = (wxUserId: number) => {
  return http.request<ResponseData<MembershipTagMemberDTO[]>>("get", `/membership/tagMembers/byWxUser/${wxUserId}`);
};

/**
 * 获取用户的所有标签
 */
export const getUserTagsApi = (params: { corpid: string; ab98UserId?: number; wxUserId?: number }) => {
  return http.request<ResponseData<MembershipTagMemberDTO[]>>("get", "/membership/tagMembers/userTags", { params });
};