/**
 * 用户认证相关API
 */

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

export interface LoginParams {
  username: string;
  password: string;
}

export interface LoginResponse {
  token: string;
  userInfo: any;
}

/**
 * 登录
 */
export async function login(params: LoginParams): Promise<LoginResponse> {
  const response = await post<LoginResponse>('/login', params);
  setToken(response.token);
  setUserInfo(response.userInfo);
  return response;
}

/**
 * 退出登录
 */
export async function logout(): Promise<void> {
  await post('/login/logout');
}

/**
 * 获取当前用户信息
 */
export async function getCurrentUser(): Promise<any> {
  return get('/login/info');
}

/**
 * 修改密码
 */
export async function changePassword(oldPassword: string, newPassword: string): Promise<void> {
  await post('/user/change-password', { oldPassword, newPassword });
}
