import { request } from '../../utils/axios';

export interface ProfileVO {
  id: number;
  username: string;
  nickname: string;
  dept: {
    id: number;
    name: string;
  };
  roles: {
    id: number;
    name: string;
  }[];
  posts: {
    id: number;
    name: string;
  }[];
  socialUsers: {
    type: number;
    openid: string;
  }[];
  email: string;
  mobile: string;
  sex: number;
  avatar: string;
  status: number;
  remark: string;
  loginIp: string;
  loginDate: Date | string;
  createTime: Date | string;
}

export interface UserProfileUpdateReqVO {
  nickname?: string;
  email?: string;
  mobile?: string;
  sex?: number;
  avatar?: string;
}

// 查询用户个人信息
export const getUserProfile = async (): Promise<ProfileVO> => {
  const response = await request.get({
    url: '/system/user/profile/get',
  });
  return response?.data || response;
};

// 修改用户个人信息
export const updateUserProfile = async (data: UserProfileUpdateReqVO): Promise<void> => {
  await request.put({
    url: '/system/user/profile/update',
    data,
  });
};

// 用户密码重置
export const updateUserPassword = async (oldPassword: string, newPassword: string): Promise<void> => {
  await request.put({
    url: '/system/user/profile/update-password',
    data: {
      oldPassword: oldPassword,
      newPassword: newPassword,
    },
  });
};

// 导出 API 对象
export const userProfileApi = {
  getUserProfile,
  updateUserProfile,
  updateUserPassword,
};

