import { apiClient } from "./client";
import { endpoints } from "./endpoints";
import type { ApiUser } from "./types";

// The shared key the Zoho-backed endpoint expects. The verify token is not a
// JWT (no dot segments) and is never sent as a Bearer header, so this endpoint
// authenticates by email + AUTH_KEY instead. Overridable via env.
const AUTH_KEY =
  process.env.NEXT_PUBLIC_API_AUTH_KEY ??
  "zm6Bm=/:9F(k;b]i>l^/@s078%&@#$$994~1d!^hDh$0GnBpFM=@alOq:Q";

type GetCurrentResponse = {
  user?: ApiUser;
  token?: string;
};

export const usersApi = {
  // Fetches the live backer record (incl. Status) for an email. Same payload
  // shape as the OTP verify response.
  getCurrent: async (email: string): Promise<ApiUser | undefined> => {
    const res = await apiClient.post<GetCurrentResponse>(
      endpoints.users.getCurrent,
      { email, AUTH_KEY }
    );
    return res.data?.user;
  },
};
