import { api } from '@shared/api/base'
import { getCsrfToken } from '@shared/api/csrf'

export interface RegistrationStartPayload {
  email: string
}

export interface RegistrationCompletePayload {
  email: string
  password: string
  confirm_password: string
  code: string
}

export interface LoginStartPayload {
  email: string
}

export interface LoginCompletePayload {
  email: string
  password: string
  code: string
}

export interface AuthResponse {
  access_token: string
}

export function registrationStart(payload: RegistrationStartPayload): Promise<void> {
  return api.post<void>('/auth/registration/start', payload)
}

export function registrationComplete(payload: RegistrationCompletePayload): Promise<AuthResponse> {
  return api.post<AuthResponse>('/auth/registration/complete', payload)
}

export function loginStart(payload: LoginStartPayload): Promise<void> {
  return api.post<void>('/auth/login/start', payload)
}

export function loginComplete(payload: LoginCompletePayload): Promise<AuthResponse> {
  return api.post<AuthResponse>('/auth/login/complete', payload)
}

export async function logout(): Promise<void> {
  const csrfToken = await getCsrfToken()
  return api.post<void>('/auth/logout', { _csrf: csrfToken })
}
