import type { Route } from './+types'

export const loader = proxy
export const action = proxy

const maxAge = 3600 * 8 // 8 hours

async function proxy({
  request,
  context
}: Route.ActionArgs): Promise<Response> {
  const pathname = new URL(request.url).pathname
  const url = new URL(pathname, context.cloudflare.env.ISSUER_URL)
  const headers = new Headers(request.headers)

  const shouldCache =
    request.method === 'GET' && pathname.startsWith('/.well-known/')

  const cache = caches.default
  const cacheKey = new Request(url.toString(), request)

  if (shouldCache) {
    const cached = await cache.match(cacheKey)
    if (cached) {
      return cached
    }
  }

  const response = await fetch(url.toString(), {
    method: request.method,
    headers,
    ...(['GET', 'HEAD'].includes(request.method)
      ? {}
      : { body: await request.text() })
  })

  if (shouldCache && response.ok) {
    const headers_ = new Headers(response.headers)
    headers_.set('Cache-Control', `public, max-age=${maxAge}`)

    const cacheResponse = new Response(response.clone().body, {
      status: response.status,
      headers: headers_
    })

    // Store asynchronously (don’t block response)
    context.cloudflare.ctx.waitUntil(cache.put(cacheKey, cacheResponse))
  }

  const contentType = response.headers.get('content-type') || ''
  if (
    contentType.includes('application/json') ||
    contentType.startsWith('text/')
  ) {
    return new Response(await response.text(), {
      status: response.status,
      headers: response.headers
    })
  }

  return response
}
