import { get, getL, post, postL } from "./http";

const apis = {
  LIST: "/api/luckRoom",
  DETAIL: "/api/luckRoom/detail",
  JOIN: "/api/luckRoom/participate",
  RECORD: "/api/luckRoom/participateRecordMy",
  LIST_CREATE: "/api/luckRoom/myCreate",
  CREATE: "/api/luckRoom/create",
  ROOM_AUDIT_RECORD: "/api/luckRoom/roomParticipateRecord",
  ROOM_AUDIT_PASS: "/api/luckRoom/auditPass",
  ROOM_AUDIT_UNPASS: "/api/luckRoom/auditUnpass",
  WINNINGRECORD: "/api/luckRoom/winningTheLotteryRecord",
  LUCKNUMLIST: "/api/luckRoom/mySign",
  ROOM_TYPE: "/api/luckRoom/roomType",
  DATE_TIME_SCOPE: "/api/activity/dollMachine/dateTimeScope", // 用于首页活动时间判断

  // Wish APIs
  // Wish APIs (Substitute Goods)
  WISH_LIST: "/api/substituteGoods/list",
  WISH_PREVIEW: "/api/substituteOrder/preSubmit",
  WISH_SUBMIT: "/api/substituteOrder/submit",
};

export const getWealList = async (
  current: number,
  size: number,
  keyword?: string,
  type?: string,
  loading = false,
) => {
  const res = await post(
    apis.LIST,
    { current, size, keyword, type },
    { loading },
  );
  return res.data;
};

export const getWealDetail = async (roomId: string, loading = false) => {
  const res = await get(apis.DETAIL, { roomId }, { loading });
  return res.data;
};

export const joinWealRoom = async (
  roomId: string,
  password = "",
  shareId?: string,
) => {
  const res = await getL(apis.JOIN, { roomId, password, shareId });
  return res;
};

export const getWealRecord = async (
  current: number,
  size: number,
  loading = false,
) => {
  const res = await post(apis.RECORD, { current, size }, { loading });
  return res.data;
};

export const getWinningRecord = async (roomId: string) => {
  const res = await get(apis.WINNINGRECORD, { roomId });
  return res.data;
};

export const getLuckNumList = async (roomId: string) => {
  const res = await getL(`${apis.LUCKNUMLIST}?roomId=${roomId}`);
  return res.data;
};

export const getRoomTypePermission = async () => {
  const res = await getL(apis.ROOM_TYPE);
  return res.data;
};

export const getDateTimeScope = async () => {
  const res = await get(apis.DATE_TIME_SCOPE);
  return res.data;
};

// 后续如有创建相关需求可继续添加
export const getMyCreateList = async (
  current: number,
  size: number,
  loading = false,
) => {
  const res = await post(apis.LIST_CREATE, { current, size }, { loading });
  return res.data;
};

export const createRoom = async (params: {
  description: string;
  inventoryIds: string[];
  luckTime: string;
  name: string;
  password?: string;
  participateMode?: number;
}) => {
  console.log("Service: createRoom called", params);
  try {
    const res = await post(apis.CREATE, params, { loading: true });
    console.log("Service: createRoom response", res);
    return res;
  } catch (error) {
    console.error("Service: createRoom error", error);
    return { success: false, msg: "网络或未知错误" };
  }
};

// 获取审核记录列表
export const roomAuditRecord = async (
  current: number,
  size: number,
  roomId: string,
  auditStatus: number,
) => {
  const res = await post(apis.ROOM_AUDIT_RECORD, {
    current,
    size,
    roomId,
    auditStatus,
  });
  return res.data;
};

// 审核通过
export const roomAuditPass = async (id: string) => {
  const res = await postL(apis.ROOM_AUDIT_PASS, { id });
  return res.success;
};

// 审核拒绝
export const roomAuditUnpass = async (id: string) => {
  const res = await postL(apis.ROOM_AUDIT_UNPASS, { id });
  return res.success;
};

export default {
  getWealList,
  getWealDetail,
  joinWealRoom,
  getWealRecord,
  getWinningRecord,
  getLuckNumList,
  getRoomTypePermission,
  getDateTimeScope,
  getMyCreateList,
  createRoom,
  roomAuditRecord,
  roomAuditPass,
  roomAuditUnpass,

  // 扭蛋机相关 API
  catchDollDetail: async () => {
    const res = await get("/api/activity/dollMachine/detail");
    return res;
  },
  dollLottery: async (params: { quantity: number }) => {
    const res = await post("/api/activity/dollMachine/participate", params);
    return res;
  },
  prizeResult: async (
    params: { current: number; size: number },
    loading = false,
  ) => {
    const res = await post(
      "/api/activity/dollMachine/pageAllParticipate",
      params,
      { loading },
    );
    return res.data;
  },

  // 祈愿相关 API (置换)
  getWishList: async () => {
    const res = await post(apis.WISH_LIST);
    return res.data;
  },
  wishPreview: async (params: {
    substituteGoodsId: string;
    inventoryIds: string[];
  }) => {
    const res = await post(apis.WISH_PREVIEW, params);
    return res.data;
  },
  wishSubmit: async (params: {
    substituteGoodsId: string;
    inventoryIds: string[];
  }) => {
    const res = await post(apis.WISH_SUBMIT, params);
    return res;
  },
};
