import httpApi from "@/api/indexApi";

/** 通知列表请求参数（与原有通知页面逻辑一致） */
export interface GetNoticeListParams {
  pageNum: number;
  pageSize: number;
  receiverId: string;
  noticeType: number;
}

/** 通知列表项 */
export interface NoticeRecord {
  id: number;
  title?: string;
  createdTime?: string;
  context?: string;
  content?: string;
  isRead?: boolean;
}

/** 通知列表响应 */
export interface GetNoticeListRes {
  records?: NoticeRecord[];
  list?: NoticeRecord[];
  total?: number;
}

/**
 * 头部通知弹窗 - 系统通知列表（独立接口，不修改原有 homeEntry）
 * GET /alienStorePlatform/notice/getNoticeList
 */
export const getNoticeListForHeader = (params: GetNoticeListParams) => {
  return httpApi.get<GetNoticeListRes>(`/alienStorePlatform/notice/getNoticeList`, params, {
    loading: false
  });
};

/**
 * 标记通知已读（alien-store 服务）
 * GET /alienStore/notice/readNoticeById?id=xxx
 */
export const readNoticeById = (params: { id: number | string }) => {
  return httpApi.get<unknown>(`/alienStore/notice/readNoticeById`, params, {
    loading: false
  });
};

/**
 * 删除通知（与商家端一致）
 * GET /alienStore/notice/deleteNoticeById?id=xxx
 */
export const deleteNoticeById = (params: { id: number | string }) => {
  return httpApi.get<unknown>(`/alienStore/notice/deleteNoticeById`, params, {
    loading: false
  });
};

/**
 * 按类型查询未读通知数量
 * GET /alienStore/notice/countUnreadByType?noticeType=0|1|2&receiverId=xxx（alien-store）
 * noticeType: 0-与我相关 1-系统通知 2-订单提醒
 */
export const getCountUnreadByType = (params: { noticeType: number; receiverId: string }) => {
  return httpApi.get<number>(`/alienStore/notice/countUnreadByType`, params, {
    loading: false
  });
};

/** 未关注人消息单条（getNoFriendMessage 返回 data 项） */
export interface NoFriendMessageItem {
  id?: number;
  senderId?: string | null;
  senderName?: string | null;
  receiverId?: string | null;
  content?: string;
  type?: string;
  isRead?: number;
  createdTime?: string;
  userName?: string | null;
  userImage?: string | null;
  storeImg?: string | null;
  senderImg?: string | null;
  notReadCount?: number;
  [key: string]: any;
}

/**
 * 未关注人消息列表
 * GET /message/getNoFriendMessage?receiverId=xxx（alien-store）
 */
export const getNoFriendMessage = (params: { receiverId: string }) => {
  return httpApi.get<NoFriendMessageItem | NoFriendMessageItem[]>(`/alienStore/message/getNoFriendMessage`, params, {
    loading: false
  });
};

/**
 * 消息列表（返回结构与 getNoFriendMessage 一致）
 * GET /message/getMessageList?receiverId=xxx&friendType=0
 */
export const getMessageList = (params: { receiverId: string; friendType?: number }) => {
  const { receiverId, friendType = 0 } = params;
  return httpApi.get<NoFriendMessageItem | NoFriendMessageItem[]>(
    `/alienStore/message/getMessageList`,
    {
      receiverId,
      friendType
    },
    {
      loading: false
    }
  );
};

/**
 * 未读消息数量（与商家端 tabbar getMessageNoRead 一致）
 * GET /message/getAllNoReadCount?receiverId=xxx（alien-store）
 */
export const getMessageNoRead = (params: { receiverId: string }) => {
  return httpApi.get<number>(`/alienStore/message/getAllNoReadCount`, params, {
    loading: false
  });
};

/**
 * 陌生人/未关注人消息未读数量（与商家端 getStrangerMessageNum 一致）
 * GET /message/getStrangerMessageNum?receiverId=xxx（alien-store）
 * 返回 { notReadCount, userName?, createdTime? } 或类似结构
 */
export const getStrangerMessageNum = (params: { receiverId: string }) => {
  return httpApi.get<{ notReadCount?: number; userName?: string; createdTime?: string; [key: string]: any }>(
    `/alienStore/message/getStrangerMessageNum`,
    params,
    { loading: false }
  );
};

/**
 * 删除未关注人消息（与商家端 message-notFriend 一致）
 * GET /message/deleteMessageByPhoneId
 * 参数 receiverId: 当前商户(store_xxx) senderId: 聊天方(store_xxx)
 */
export const deleteMessageByPhoneId = (params: { receiverId: string; senderId: string }) => {
  return httpApi.get<unknown>(`/alienStore/message/deleteMessageByPhoneId`, params, {
    loading: false
  });
};
