import type { UserInfo, UserToken } from '#/entity'
import Request from '../request'

export interface SignInReq {
  username: string
  password: string
}

export interface SignUpReq extends SignInReq {
  email: string
}
export type SignInRes = UserToken & { user: UserInfo }

export enum UserApi {
  SignIn = '/auth/signin',
  SignUp = '/auth/signup',
  Logout = '/auth/logout',
  Refresh = '/auth/refresh',
  User = '/user'
}

const signin = (data: SignInReq) => Request.post<SignInRes>({ url: UserApi.SignIn, data })
const signup = (data: SignUpReq) => Request.post<SignInRes>({ url: UserApi.SignUp, data })
const logout = () => Request.get({ url: UserApi.Logout })
const findById = (id: string) => Request.get<UserInfo[]>({ url: `${UserApi.User}/${id}` })

export default {
  signin,
  signup,
  findById,
  logout
}
