/**
 * Setup wizard API functions.
 *
 * Wraps calls to GET /api/setup and POST /api/setup.
 */

import { api } from "./client";
import { ENDPOINTS } from "./endpoints";
import type {
  SetupRequest,
  SetupResponse,
  SetupStatusResponse,
  SetupTimezoneResponse,
} from "../types/setup";

/**
 * Check whether the initial setup has been completed.
 *
 * @returns Setup status response with a `completed` boolean.
 */
export async function getSetupStatus(): Promise<SetupStatusResponse> {
  return api.get<SetupStatusResponse>(ENDPOINTS.setup);
}

/**
 * Submit the initial setup configuration.
 *
 * @param data - Setup request payload.
 * @returns Success message from the API.
 */
export async function submitSetup(data: SetupRequest): Promise<SetupResponse> {
  return api.post<SetupResponse>(ENDPOINTS.setup, data);
}

/**
 * Fetch the IANA timezone configured during setup.
 *
 * Used by the frontend to convert UTC timestamps to the local timezone
 * chosen by the administrator.
 *
 * @returns The configured timezone identifier (e.g. `"Europe/Berlin"`).
 */
export async function fetchTimezone(signal?: AbortSignal): Promise<SetupTimezoneResponse> {
  return api.get<SetupTimezoneResponse>(ENDPOINTS.setupTimezone, signal);
}
