/**
 * RRYM / 002 — SULSYS
 * Deployment-level constants. Nothing here is a claim about the business.
 */

/**
 * The canonical origin, in order of authority:
 *
 *  1. `NEXT_PUBLIC_SITE_URL`, so sulsys.net can be switched on by setting one
 *     variable rather than by editing code.
 *  2. Vercel's own production domain, for a deployment where nobody has set
 *     that variable yet. This is the branch that matters: canonicals, hreflang
 *     and the sitemap are all absolute URLs, and before this they shipped to
 *     production reading `http://localhost:3000` — hreflang pointing at
 *     localhost is worse than no hreflang at all, and nothing in the build
 *     said a word about it. `VERCEL_PROJECT_PRODUCTION_URL` is the stable
 *     project domain, not the per-deployment one, which is what a canonical
 *     has to name.
 *  3. The per-deployment URL on a preview, so a preview is at least
 *     self-consistent. Previews are `noindex` regardless.
 *  4. localhost, for development and the test suite.
 *
 * All of these are read at build time, and every caller is server-side.
 */
function resolveSiteUrl(): string {
  const configured = process.env.NEXT_PUBLIC_SITE_URL;
  if (configured) return configured;

  const production = process.env.VERCEL_PROJECT_PRODUCTION_URL;
  if (production) return `https://${production}`;

  const deployment = process.env.VERCEL_URL;
  if (deployment) return `https://${deployment}`;

  return "http://localhost:3000";
}

export const SITE_URL = resolveSiteUrl().replace(/\/$/, "");

/**
 * And the invariant, checked at build time rather than discovered in Search
 * Console: a production build whose origin is still localhost must not ship.
 * This is the failure the branches above exist to prevent, so it fails loudly
 * if it ever recurs by another route.
 */
if (process.env.VERCEL_ENV === "production" && SITE_URL.startsWith("http://localhost")) {
  throw new Error(
    "Production build has no canonical origin: set NEXT_PUBLIC_SITE_URL. " +
      "Every canonical, hreflang and sitemap URL would read http://localhost:3000.",
  );
}

/** Plausible is loaded only when a domain is configured (Blueprint 12). */
export const PLAUSIBLE_DOMAIN = process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN ?? "";

/**
 * Previews must not be indexed, and the form must not reach Sulsys from a
 * preview (Blueprint 14). `VERCEL_ENV` is set by Vercel; locally it is absent,
 * which also counts as "not production".
 */
export const IS_PRODUCTION = process.env.VERCEL_ENV === "production";

/**
 * The RRYM credit line in the footer is off until Kujto agrees (Blueprint 07).
 */
export const SHOW_RRYM_CREDIT = process.env.NEXT_PUBLIC_RRYM_CREDIT === "true";

/**
 * Build date for the footer record line. Unlike a product version — which is
 * UNKNOWN and renders nothing — this is a real, checkable value.
 */
export const BUILD_DATE =
  process.env.NEXT_PUBLIC_BUILD_DATE ?? new Date().toISOString().slice(0, 10);

export function absoluteUrl(path: string): string {
  return `${SITE_URL}${path.startsWith("/") ? path : `/${path}`}`;
}
