// Read-only price catalog for the portal. Partner-staff need to see active
// prices to render the customer-create wizard's Plan step. The platform-api
// /prices GET is open to any authenticated JWT (operator-only mutations are
// guarded separately), so a portal-aud token works fine here.

import { getUserSession } from 'nuxt-oidc-auth/runtime/server/utils/session.js'

export default defineEventHandler(async (event) => {
  const session = await getUserSession(event).catch(() => null)
  const accessToken = (session as { accessToken?: string } | null)?.accessToken
  if (!accessToken) {
    throw createError({ statusCode: 401, statusMessage: 'Not signed in' })
  }

  const base = process.env.PLATFORM_API_INTERNAL_URL ?? 'http://platform-api:3001'
  try {
    return await $fetch(`${base}/prices`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    })
  } catch (err: unknown) {
    const e = err as { statusCode?: number; data?: unknown }
    throw createError({ statusCode: e.statusCode ?? 500, data: e.data })
  }
})
