/**
 * Instagram OAuth Client ID — public; same value as modstagram-back INSTAGRAM_CLIENT_ID.
 * Left empty until a Meta App is configured; InstagramSignInButton renders nothing
 * when this (and the runtime /api/auth/config value) are both empty.
 */
export const INSTAGRAM_OAUTH_CLIENT_ID = "";

/** Must exactly match INSTAGRAM_REDIRECT_URI configured on the backend and in the Meta App. */
export function getInstagramRedirectUri(): string {
  if (typeof window === "undefined") return "";
  return `${window.location.origin}/auth/instagram/callback`;
}

export type InstagramAuthMode = "login" | "register" | "link";

/**
 * Only the scope needed to read the connected account's basic profile.
 * Media-read scope is added in M2 once content import is built.
 */
const INSTAGRAM_SCOPE = "instagram_business_basic";

export function buildInstagramAuthorizeUrl({
  clientId,
  mode,
  nonce,
}: {
  clientId: string;
  mode: InstagramAuthMode;
  nonce: string;
}): string {
  const state = encodeURIComponent(JSON.stringify({ mode, nonce }));
  const params = new URLSearchParams({
    client_id: clientId,
    redirect_uri: getInstagramRedirectUri(),
    scope: INSTAGRAM_SCOPE,
    response_type: "code",
    state,
  });

  return `https://www.instagram.com/oauth/authorize?${params.toString()}`;
}
