/**
 * Auth API — login, register, token lifecycle, TOTP, password reset, preferences, quota.
 *
 * Consumers: stores/auth.js, SettingsAccountTab.vue, LoginView.vue,
 *            TotpEnrollment.vue, PasswordResetView.vue
 */

import { request, jsonRequest } from './utils.js'

export function login(body) {
  return jsonRequest('/api/auth/login', 'POST', body)
}

export function register(body) {
  return jsonRequest('/api/auth/register', 'POST', body)
}

export function refreshToken() {
  // No body — httpOnly cookie sent automatically via credentials: 'include'
  return request('/api/auth/refresh', { method: 'POST' })
}

export function logout() {
  return request('/api/auth/logout', { method: 'POST' })
}

export function logoutAll() {
  return request('/api/auth/logout-all', { method: 'POST' })
}

export function getMe() {
  return request('/api/auth/me')
}

export function changePassword(body) {
  return jsonRequest('/api/auth/change-password', 'POST', body)
}

export function totpSetup() {
  return request('/api/auth/totp/setup')
}

export function totpEnable(code) {
  return jsonRequest('/api/auth/totp/enable', 'POST', { code })
}

export function totpDisable() {
  return request('/api/auth/totp', { method: 'DELETE' })
}

export function passwordResetRequest(email) {
  return jsonRequest('/api/auth/password-reset', 'POST', { email })
}

export function passwordResetConfirm(token, newPassword) {
  return jsonRequest('/api/auth/password-reset/confirm', 'POST', {
    token,
    new_password: newPassword,
  })
}

export function getMyPreferences() {
  return request('/api/auth/me/preferences')
}

export function updateMyPreferences(payload) {
  return jsonRequest('/api/auth/me/preferences', 'PATCH', payload)
}

export function getMyQuota() {
  return request('/api/auth/me/quota')
}
