/**
 * 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 } from './utils.js'

export function login(body) {
  return request('/api/auth/login', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(body),
  })
}

export function register(body) {
  return request('/api/auth/register', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(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 request('/api/auth/change-password', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(body),
  })
}

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

export function totpEnable(code) {
  return request('/api/auth/totp/enable', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ code }),
  })
}

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

export function passwordResetRequest(email) {
  return request('/api/auth/password-reset', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ email }),
  })
}

export function passwordResetConfirm(token, newPassword) {
  return request('/api/auth/password-reset/confirm', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ token, new_password: newPassword }),
  })
}

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

export function updateMyPreferences(payload) {
  return request('/api/auth/me/preferences', {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload),
  })
}

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