import { post, get } from '@/utils/request';

export function sendCode(phone: string) {
  return post('/api/app/auth/send-code', { phone });
}

export function loginByPhone(phone: string, code: string, inviteCode?: string) {
  return post('/api/app/auth/login/phone', { phone, code, inviteCode });
}

export function loginByPassword(phone: string, password: string, inviteCode?: string) {
  return post('/api/app/auth/login/password', { phone, password, inviteCode });
}

export function loginByWechat(code: string, nickname?: string, avatar?: string, inviteCode?: string) {
  return post('/api/app/auth/login/wechat', { code, nickname, avatar, inviteCode });
}

export function loginByWechatPhone(code: string, encryptedData: string, iv: string, inviteCode?: string) {
  return post('/api/app/auth/login/wechat-phone', { code, encryptedData, iv, inviteCode });
}

export function register(data: { phone: string; code: string; password?: string; nickname?: string; inviteCode?: string }) {
  return post('/api/app/auth/register', data);
}

export function refreshToken(refreshToken: string) {
  return post('/api/app/auth/refresh', { refreshToken });
}

export function getUserProfile() {
  return get('/api/app/profile/profile');
}

export function updateUserProfile(data: { nickname?: string; avatar?: string }) {
  return post('/api/app/profile/profile', data);
}

export function uploadAvatar(filePath: string) {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: import.meta.env.VITE_API_BASE_URL + '/api/app/profile/avatar',
      filePath,
      name: 'file',
      header: {
        'Authorization': 'Bearer ' + uni.getStorageSync('token')
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(JSON.parse(res.data));
        } else {
          reject(new Error('上传失败'));
        }
      },
      fail: reject
    });
  });
}

export function verifyIdentity(data: { realName: string; idCard: string }) {
  return post('/api/app/profile/verify', data);
}

export function getVerifyStatus() {
  return get('/api/app/profile/verify/status');
}
