/**
 * Central builder for team / competition / country image URLs.
 *
 * Images are served from a Cloudflare R2 bucket fronted by the
 * `workers/image-proxy` Worker, which lazily mirrors each image from the
 * upstream provider (file.mackolikfeeds.com) into R2 on first request.
 *
 * Set IMAGE_BASE_URL (no trailing slash, e.g. https://files.example.com)
 * to serve from the Worker. When unset, URLs point directly at the
 * upstream provider so behaviour is unchanged until the bucket is live.
 */
const DEFAULT_IMAGE_BASE_URL = "https://file.mackolikfeeds.com";

function imageBaseUrl(): string {
  return (
    process.env.IMAGE_BASE_URL?.replace(/\/+$/, "") || DEFAULT_IMAGE_BASE_URL
  );
}

export function teamLogoUrl(teamId?: string | null): string | undefined {
  if (!teamId) return undefined;
  return `${imageBaseUrl()}/teams/${teamId}`;
}

export function competitionLogoUrl(
  competitionId?: string | null,
): string | undefined {
  if (!competitionId) return undefined;
  return `${imageBaseUrl()}/competitions/${competitionId}`;
}

export function countryFlagUrl(countryId?: string | null): string | undefined {
  if (!countryId) return undefined;
  return `${imageBaseUrl()}/areas/${countryId}`;
}
