import { requestClient } from '#/api/request';

export namespace ProfileApi {
  /** 个人信息 */
  export interface ProfileInfo {
    id?: number;
    username?: string;
    nickname?: string;
    email?: string | null;
    avatar?: string | null;
    role?: string;
    school_id?: number | null;
    status?: number;
    [key: string]: any;
  }

  /** 更新个人信息参数 */
  export interface UpdateProfileParams {
    nickname?: string;
    email?: string;
    avatar?: File | string;
  }

  /** 修改密码参数 */
  export interface UpdatePasswordParams {
    old_password: string;
    new_password: string;
    confirm_password: string;
  }
}

/**
 * 获取个人信息
 * GET /api/admin/profile/info
 */
export async function getProfileInfoApi() {
  return requestClient.get<ProfileApi.ProfileInfo>('/api/admin/profile/info');
}

/**
 * 更新个人信息
 * POST /api/admin/profile/update
 */
export async function updateProfileApi(data: ProfileApi.UpdateProfileParams) {
  const formData = new FormData();
  if (data.nickname) {
    formData.append('nickname', data.nickname);
  }
  // email 可能是空字符串，需要明确处理
  if (data.email !== undefined && data.email !== null && data.email !== '') {
    formData.append('email', data.email);
  } else if (data.email === '') {
    // 如果传了空字符串，可能需要传空值，根据后端需求调整
    formData.append('email', '');
  }
  if (data.avatar instanceof File) {
    formData.append('avatar', data.avatar);
  } else if (data.avatar) {
    formData.append('avatar', data.avatar);
  }
  return requestClient.post<ProfileApi.ProfileInfo>('/api/admin/profile/update', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

/**
 * 修改密码
 * POST /api/admin/profile/updatePassword
 */
export async function updatePasswordApi(data: ProfileApi.UpdatePasswordParams) {
  return requestClient.post('/api/admin/profile/updatePassword', data);
}

