import { apiGet, apiPost } from './http'
import type { User } from '@/types/api'

export interface LoginPayload {
  email: string
  password: string
  captcha?: string
}

export interface LoginResult {
  token: string
  token_type: string
  expires_in: number
  user: User
}

export function login(payload: LoginPayload) {
  return apiPost<LoginResult>('/api/auth/login', payload)
}

export function register(payload: {
  name: string
  email: string
  password: string
  password_confirmation: string
  invite_code: string
}) {
  return apiPost<LoginResult>('/api/auth/register', payload)
}

export function me() {
  return apiGet<User>('/api/auth/me')
}

export function captcha() {
  return apiGet<{ captcha: string; expires_in: number }>('/api/auth/captcha')
}

export function forgotPassword(email: string) {
  return apiPost<{ token?: string }>('/api/auth/forgot-password', { email })
}

export function resetPassword(payload: {
  email: string
  token: string
  password: string
  password_confirmation: string
}) {
  return apiPost('/api/auth/reset-password', payload)
}

export function installStatus() {
  return apiGet<{ installed: boolean; database: string }>('/api/install/status')
}

export function runInstall(payload: { admin_email: string; admin_password: string; fresh?: boolean }) {
  return apiPost('/api/install/run', payload)
}
