/**
 * The absolute origin this deployment is actually served from.
 *
 * Canonical tags, the sitemap, robots.txt and the Open Graph image all need an
 * absolute URL, and it has to be the host the visitor really reached. Hardcoding
 * the live domain means a preview build hands out canonicals pointing at a
 * different site and social previews that resolve to an image which is not there.
 *
 * Resolution order:
 *
 *   NEXT_PUBLIC_SITE_URL          set this once threestarspk.com points here
 *   VERCEL_PROJECT_PRODUCTION_URL the project's stable production hostname
 *   VERCEL_URL                    per deployment hostname, so previews self describe
 *   http://localhost:3000         local development
 *
 * These routes are prerendered, so the value is resolved at build time. All four
 * variables are present during a Vercel build.
 */
function resolveSiteUrl(): string {
  const explicit = process.env.NEXT_PUBLIC_SITE_URL;
  if (explicit) return explicit.replace(/\/$/, "");

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

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

export const SITE_URL = resolveSiteUrl();

/**
 * Whether search engines should be allowed in.
 *
 * Preview deployments must stay out of the index. They are throwaway hostnames
 * carrying the same copy as production, which is the textbook way to end up
 * competing against yourself in search results.
 */
export const INDEXABLE =
  process.env.VERCEL_ENV === undefined || process.env.VERCEL_ENV === "production";
