import type { APIRoute } from 'astro'

const runtimeProcess = globalThis as typeof globalThis & {
  process?: {
    env?: Record<string, string | undefined>
  }
}

function readIndexNowKey() {
  return runtimeProcess.process?.env?.INDEXNOW_KEY?.trim() || ''
}

export const prerender = false

export const GET: APIRoute = async () => {
  const key = readIndexNowKey()

  if (!key) {
    return new Response('Not Found', { status: 404 })
  }

  return new Response(key, {
    headers: {
      'Content-Type': 'text/plain; charset=utf-8',
      'Cache-Control': 'public, max-age=600',
    },
  })
}
