/**
 * Auth API client — REST endpoints with cookie sessions.
 *
 * ⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
 */

const API_BASE_URL = import.meta.env.VITE_API_URL || '';

async function apiPost<T>(path: string, body: unknown): Promise<T> {
  const response = await fetch(`${API_BASE_URL}${path}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    credentials: 'include',
    body: JSON.stringify(body),
  });

  if (!response.ok) {
    const data = await response.json().catch(() => ({}));
    throw new Error(data.detail || `Request failed: ${response.status}`);
  }

  return response.json();
}

// ── Types ──────────────────────────────────────────────────────

export interface LoginResponse {
  requires_mfa: boolean;
  user?: Record<string, unknown> | null;
}

{% if mfa_enabled %}
export interface MFASetupResponse {
  totp_uri: string;
  secret: string;
}
{% endif %}

// ── Auth API ───────────────────────────────────────────────────

export async function signup(
  {{ username_field }}: string,
  username: string,
  password: string
): Promise<{ message: string }> {
  return apiPost('/api/auth/signup', { {{ username_field }}, username, password });
}

export async function login(
  {{ username_field }}: string,
  password: string
): Promise<LoginResponse> {
  return apiPost('/api/auth/login', { {{ username_field }}, password });
}

{% if mfa_enabled %}
export async function loginMFA(
  {{ username_field }}: string,
  code: string
): Promise<LoginResponse> {
  return apiPost('/api/auth/login/mfa', { {{ username_field }}, code });
}
{% endif %}

{% if email_verification %}
export async function verifyEmail(
  token: string
): Promise<{ message: string; user: Record<string, unknown> }> {
  return apiPost('/api/auth/verify-email', { token });
}

export async function resendVerification(
  {{ username_field }}: string
): Promise<{ message: string }> {
  return apiPost('/api/auth/resend-verification', { {{ username_field }} });
}
{% endif %}

{% if password_reset %}
export async function forgotPassword(
  {{ username_field }}: string
): Promise<{ message: string }> {
  return apiPost('/api/auth/forgot-password', { {{ username_field }} });
}

export async function resetPassword(
  token: string,
  password: string
): Promise<{ message: string; user: Record<string, unknown> }> {
  return apiPost('/api/auth/reset-password', { token, password });
}
{% endif %}

{% if mfa_enabled %}
export async function setupMFA(): Promise<MFASetupResponse> {
  return apiPost('/api/auth/mfa/setup', {});
}

export async function verifyMFASetup(
  code: string
): Promise<{ message: string }> {
  return apiPost('/api/auth/mfa/verify-setup', { code });
}

export async function disableMFA(
  password: string
): Promise<{ message: string }> {
  return apiPost('/api/auth/mfa/disable', { password });
}
{% endif %}

{% for provider in oauth_providers %}
{% if provider.provider == "github" %}
export function getGitHubLoginUrl(): string {
  return `${API_BASE_URL}/api/auth/github/login`;
}
{% endif %}
{% endfor %}
