import client from './client'
import type { TokenResponse, LoginResponse, User } from '@/types'

export interface RegisterData {
  login: string
  password: string
  nickname: string
}

export interface LoginData {
  login: string
  password: string
}

export const authApi = {
  register: async (data: RegisterData): Promise<TokenResponse> => {
    const response = await client.post<TokenResponse>('/auth/register', data)
    return response.data
  },

  login: async (data: LoginData): Promise<LoginResponse> => {
    const response = await client.post<LoginResponse>('/auth/login', data)
    return response.data
  },

  verify2FA: async (sessionId: number, code: string): Promise<TokenResponse> => {
    const response = await client.post<TokenResponse>('/auth/2fa/verify', null, {
      params: { session_id: sessionId, code }
    })
    return response.data
  },

  me: async (): Promise<User> => {
    const response = await client.get<User>('/auth/me')
    return response.data
  },
}
