import api, { unwrap } from './request';

export interface LoginReq {
  loginId: string;
  password: string;
}

export interface LoginResp {
  accessToken: string;
  refreshToken?: string;
  accessTokenExpiresIn: number;
  userId: number;
  loginId: string;
  userName: string;
  roles: string[];
}

export interface UserResp {
  userId: number;
  loginId: string;
  userName: string;
  email?: string;
  phone?: string;
  agentId?: number;
  agentName?: string;
  orgName?: string;
  status: string;
  roleCodes?: string[];
}

export const authApi = {
  login: (req: LoginReq) => unwrap<LoginResp>(api.post('/api/auth/login', req)),
  refresh: (refreshToken: string) =>
    unwrap<LoginResp>(api.post('/api/auth/refresh', { refreshToken })),
  me: () => unwrap<UserResp>(api.get('/api/auth/me')),
  logout: () => unwrap<void>(api.post('/api/auth/logout')),
  changePassword: (oldPassword: string, newPassword: string) =>
    unwrap<void>(api.post('/api/auth/password', { oldPassword, newPassword })),
};
