import type { ApiResponse } from './types/common';
import type { LoginAccountParams, LoginResponse, UserInfo } from './types/auth';

const delay = (ms = 500) => new Promise(r => setTimeout(r, ms));

/** 登录接口 */
export async function login(data: LoginAccountParams): Promise<ApiResponse<LoginResponse>> {
  await delay();

  if (data.username === 'admin' && data.password === '123456') {
    return {
      code: 200,
      message: 'ok',
      data: {
        token: 'token-' + Date.now(),
      },
    };
  }

  return {
    code: 401,
    message: '用户名或密码错误',
    data: { token: '' },
  };
}

/** 获取用户信息 */
export async function getUserInfo(): Promise<ApiResponse<UserInfo>> {
  await delay();
  return {
    code: 200,
    message: 'ok',
    data: {
      id: '1',
      username: 'admin',
      nickname: '管理员',
      avatar: '',
    },
  };
}

/** 退出登录 */
export async function logout(): Promise<ApiResponse<void>> {
  await delay();
  return {
    code: 200,
    message: 'ok',
    data: undefined,
  };
}
