type PublicConfig = {
  siteUrl?: string;
  assetVersion?: string;
  enableNavHoverLine: boolean;
  umami?: {
    scriptUrl: string;
    websiteId: string;
  };
};

type IngestConfig = {
  youtubeChannelId?: string;
  youtubeApiKey?: string;
  podcastRssUrl?: string;
  instagramPostUrlsFile: string;
  wordpressBaseUrl?: string;
  wordpressUsername?: string;
  wordpressAppPassword?: string;
  cacheRedisUrl?: string;
  cacheRedisHost?: string;
  cacheRedisPort?: number;
  cacheRedisDb?: number;
  cacheDefaultTtlSeconds?: number;
};

export function getPublicConfig(): PublicConfig {
  const siteUrl = import.meta.env.PUBLIC_SITE_URL;
  const assetVersion = import.meta.env.PUBLIC_ASSET_VERSION;
  const enableNavHoverLine = import.meta.env.PUBLIC_ENABLE_NAV_HOVER_LINE !== "false";
  const scriptUrl = import.meta.env.PUBLIC_UMAMI_SCRIPT_URL;
  const websiteId = import.meta.env.PUBLIC_UMAMI_WEBSITE_ID;

  return {
    siteUrl,
    assetVersion,
    enableNavHoverLine,
    umami: scriptUrl && websiteId ? { scriptUrl, websiteId } : undefined,
  };
}

// Ingestion scripts run under Node (not inside Astro runtime).
export function getIngestConfigFromEnv(env: NodeJS.ProcessEnv): IngestConfig {
  return {
    youtubeChannelId: env.YOUTUBE_CHANNEL_ID,
    youtubeApiKey: env.YOUTUBE_API_KEY,
    podcastRssUrl: env.PODCAST_RSS_URL,
    instagramPostUrlsFile: env.INSTAGRAM_POST_URLS_FILE || "content/instagram-posts.json",
    wordpressBaseUrl: env.WORDPRESS_BASE_URL,
    wordpressUsername: env.WORDPRESS_USERNAME,
    wordpressAppPassword: env.WORDPRESS_APP_PASSWORD,
    cacheRedisUrl: env.CACHE_REDIS_URL,
    cacheRedisHost: env.CACHE_REDIS_HOST,
    cacheRedisPort: env.CACHE_REDIS_PORT ? Number(env.CACHE_REDIS_PORT) : undefined,
    cacheRedisDb: env.CACHE_REDIS_DB ? Number(env.CACHE_REDIS_DB) : undefined,
    cacheDefaultTtlSeconds: env.CACHE_DEFAULT_TTL_SECONDS
      ? Number(env.CACHE_DEFAULT_TTL_SECONDS)
      : undefined,
  };
}
