// First-run install wizard: probes install state and drives admin/config/email setup with the
// one-time token printed to the container logs (carried via setupHeaders). Every write is quiet so
// the wizard renders errors inline. Only touches core's req + setupHeaders.
import { req, setupHeaders } from "./core";

export const setupApi = {
  setupStatus: (): Promise<{ configured: boolean }> => req("/api/setup/status"),
  setupInfo: (token: string): Promise<{ secrets_manageable: boolean }> =>
    req("/api/setup/info", { headers: setupHeaders(token) }, { quiet: true }),
  setupAdmin: (token: string, email: string, password: string): Promise<{ ok: boolean }> =>
    req(
      "/api/setup/admin",
      { method: "POST", headers: setupHeaders(token), body: JSON.stringify({ email, password }) },
      { quiet: true },
    ),
  setupConfig: (token: string, values: Record<string, unknown>): Promise<{ saved: string[] }> =>
    req(
      "/api/setup/config",
      { method: "POST", headers: setupHeaders(token), body: JSON.stringify(values) },
      { quiet: true },
    ),
  setupTestEmail: (token: string, to: string): Promise<{ ok: boolean }> =>
    req(
      "/api/setup/test-email",
      { method: "POST", headers: setupHeaders(token), body: JSON.stringify({ to }) },
      { quiet: true },
    ),
  setupFinish: (token: string): Promise<{ ok: boolean }> =>
    req("/api/setup/finish", { method: "POST", headers: setupHeaders(token) }, { quiet: true }),
};
