/** Canonical public site origin (no trailing slash). */
export function getSiteUrl() {
  const configured = process.env.NEXT_PUBLIC_SITE_URL?.replace(/\/$/, '');
  if (configured) return configured;
  if (typeof window !== 'undefined') return window.location.origin;
  return 'http://localhost:3000';
}

export function getApiOrigin() {
  return (
    process.env.API_URL?.replace(/\/$/, '') ||
    process.env.NEXT_PUBLIC_API_URL?.replace(/\/$/, '') ||
    'http://localhost:4000'
  );
}

export function absoluteUrl(path: string) {
  const normalized = path.startsWith('/') ? path : `/${path}`;
  return `${getSiteUrl()}${normalized}`;
}

export function absoluteUploadUrl(path?: string | null) {
  if (!path) return undefined;
  if (path.startsWith('http')) return path;
  const api = getApiOrigin();
  return `${api}${path.startsWith('/') ? path : `/${path}`}`;
}
