// 用户服务 - 对应 supermart-mini/service/user.js
import { get, postL, setToken } from './http';

const apis = {
  LOGIN: '/api/account/login',
  USER_INFO: '/api/myProfile/get',
  UPDATE_INFO: '/api/myProfile/basicInfo/update',
  UPDATE_AVATAR: '/api/myProfile/avatar/update',
  UPDATE_NICKNAME: '/api/myProfile/nickname/update',
  REAL_NAME: '/api/myProfile/basicInfo/update',
  SEND_CODE: '/api/verifycode/send',
  PARAM_CONFIG: '/param/paramConfig',
  WALLET_AMOUNT: '/api/wallet/ranking/walletAmount',
  WALLET_INFO: '/api/wallet/info',
  LOGOFF: '/api/removeAccount/submit',
  GET_HIDE: '/api/luckOrder/hide',
  SIGN_IN: '/credit/signIn',
  CREDIT_RECORD: '/credit/selectCreditRecord',
  CREDIT_SHOW: '/credit_show',
  ISLAND_SIGN_IN: '/api/activity/activityIsland/signIn',
  DISPLAY_REWARDS: '/api/activity/activityIsland/displayRewards',
  ACTIVITY_TA_LIST: '/api/activity/activityTa/list',
  ACTIVITY_TA_LEVEL: '/api/activity/activityTa/level',
  CLAIM_DAILY_REWARD: '/api/wallet/recharge/claimDailyReward',
  NEW_USER_NUM: '/api/wallet/ranking/newUserNum',
  LOGOUT: '/api/account/logout',
};

export interface UserInfo {
  id: string;
  userId?: string;
  username?: string;
  nickname: string;
  avatar: string;
  phone?: string;
  mobile?: string;
  realName?: string;
  idName?: string;
  idNum?: string;
  realNameFlag?: number;
  balance?: number;
}

export interface LoginParams {
  loginWay: string;
  mobile: string;
  verifycode: string;
}

export interface ParamConfig {
  code: string;
  data: string;
  state: number;
}

export interface LoginResult {
  success: boolean;
  needInfo?: boolean;
}

// 账号登录
export const login = async (params: LoginParams): Promise<LoginResult> => {
  const res = await postL<{ tokenInfo: { tokenValue: string }; needPerfectProfile?: boolean }>(apis.LOGIN, params);
  if (res.success && res.data?.tokenInfo?.tokenValue) {
    setToken(res.data.tokenInfo.tokenValue);
    return {
      success: true,
      needInfo: res.data.needPerfectProfile,
    };
  }
  return { success: false };
};

// 退出登录
export const logout = async (): Promise<boolean> => {
  const res = await get(apis.LOGOUT);
  return res.success;
};

// 获取用户信息
export const getUserInfo = async (): Promise<UserInfo | null> => {
  const res = await get<UserInfo>(apis.USER_INFO);
  return res.data;
};

// 更新用户信息
export const updateUserInfo = async (params: Partial<UserInfo>): Promise<boolean> => {
  const res = await postL(apis.UPDATE_INFO, params);
  return res.success;
};

// 更新头像
export const updateAvatar = async (avatar: string): Promise<boolean> => {
  const res = await postL(apis.UPDATE_AVATAR, { avatar });
  return res.success;
};

// 更新昵称
export const updateNickname = async (nickname: string): Promise<boolean> => {
  const res = await postL(apis.UPDATE_NICKNAME, { nickname });
  return res.success;
};

// 实名认证
export const realNameAuth = async (idName: string, idNum: string): Promise<boolean> => {
  const res = await postL(apis.REAL_NAME, { idName, idNum });
  return res.success;
};

// 发送验证码
export const sendVerifyCode = async (mobile: string, scene = 'LOGIN'): Promise<boolean> => {
  const res = await postL(apis.SEND_CODE, { mobile, scene });
  return res.success;
};

// 获取参数配置
export const getParamConfig = async (code: string): Promise<ParamConfig | null> => {
  const res = await get<ParamConfig>(apis.PARAM_CONFIG, { code });
  return res.data;
};

// 获取钱包余额
export const getWalletAmount = async () => {
  const res = await get(apis.WALLET_AMOUNT);
  return res;
};

// 获取钱包信息（积分等）
export const getWalletInfo = async (type: string) => {
  const res = await get(apis.WALLET_INFO, { type });
  return res.data;
};

// 注销账号
export const logoff = async (): Promise<boolean> => {
  const res = await postL(apis.LOGOFF);
  return res.success;
};

// 获取隐藏状态 (1: 没消费，0: 消费)
export const getHide = async () => {
  const res = await get(apis.GET_HIDE);
  return res.data;
};

// 签到
export const signIn = async () => {
  const res = await postL(apis.SIGN_IN);
  return res;
};

// 岛屿签到
export const islandSignIn = async () => {
  const res = await postL(apis.ISLAND_SIGN_IN);
  return res;
};

// 获取积分记录
export const getCreditRecord = async (params?: any) => {
  const res = await get(apis.CREDIT_RECORD, params);
  return res;
};

// 获取积分显示配置
export const creditShow = async () => {
  const res = await get(apis.CREDIT_SHOW);
  return res;
};

// 获取展示奖励
export const displayRewards = async () => {
  const res = await get(apis.DISPLAY_REWARDS);
  return res;
};

// 获取活动列表
export const activityTaList = async () => {
  const res = await get(apis.ACTIVITY_TA_LIST);
  return res;
};

// 获取活动等级
export const activityTaLevel = async (params?: any) => {
  const res = await get(apis.ACTIVITY_TA_LEVEL, params);
  return res;
};

// 领取每日奖励
export const claimDailyReward = async (params?: any) => {
  const res = await postL(apis.CLAIM_DAILY_REWARD, params);
  return res;
};

// 获取新用户数量
export const getNewUserNum = async (params?: any) => {
  const res = await get(apis.NEW_USER_NUM, params);
  return res;
};

// 转赠订单提交
export const transferOrderSubmit = async (params: {
  inventoryIds: string[];
  levels: string[];
  toUserShortId: string;
}): Promise<{ success: boolean; message?: string }> => {
  const res = await postL('/api/transferOrder/submit', params);
  return res;
};

export default {
  login,
  logout,
  getUserInfo,
  updateUserInfo,
  updateAvatar,
  updateNickname,
  realNameAuth,
  sendVerifyCode,
  getParamConfig,
  getWalletAmount,
  getWalletInfo,
  logoff,
  getHide,
  signIn,
  islandSignIn,
  getCreditRecord,
  creditShow,
  displayRewards,
  activityTaList,
  activityTaLevel,
  claimDailyReward,
  getNewUserNum,
};
