import { GroupType } from '@/constants/group';
import http from '@/http';
import { API } from '@/http/api';
import {
  BatchPublishGroup,
  GetGroupDetailsRequest,
  GetGroupsRequest,
  GetGroupsResponse,
  GroupDetail,
  SimpleGroupInfo,
} from '@/types/group';
import { JobDetails } from '@/types/job';
import { getUserId } from '@/utils/user';

const typeToKeyMap = {
  [GroupType.All]: 'allGroups',
  [GroupType.Joined]: 'myJoinedGroups',
  [GroupType.Created]: 'myCreatedGroups',
  [GroupType.Followed]: 'myFollowedGroups',
};

export const getInviteGroupText = (groupDetail: GroupDetail) => {
  return `请邀请我进群:${getUserId()}|${groupDetail.blGroupId}|${groupDetail.imGroupNick}|${groupDetail.robotId}`;
};

export const getConnectCustomerServiceText = (jobDetail: JobDetails, groupDetail: GroupDetail) => {
  return `请帮我对接该通告:${getUserId()}|${groupDetail.blGroupId}|${groupDetail.robotId}|${jobDetail.publisher}|${jobDetail.id}`;
};

export async function requestGroupList(data: GetGroupsRequest) {
  const type = data.type;
  const result = await http.post<GetGroupsResponse>(API.GROUPS, { data });
  const groupResults = result[typeToKeyMap[type]] || [];
  return {
    page: 1,
    pageSize: groupResults.length,
    hasMore: false,
    groupResults,
  };
}

export async function requestGroupDetail(blGroupId: GetGroupDetailsRequest['blGroupId']) {
  return await http.post<GroupDetail>(API.GROUP_DETAIL, {
    data: { blGroupId },
    contentType: 'application/x-www-form-urlencoded',
  });
}

export const requestBatchPublishGroups = async () => {
  return http.post<BatchPublishGroup[]>(API.BATCH_PUBLISH_GROUP_LIST);
};

export const requestSimpleGroupList = async (cityCode?: string) => {
  return http.post<SimpleGroupInfo[]>(API.SIMPLE_GROUP_LIST, { data: { cityCode } });
};
