// Partner team listing for the customer portal's /partner/team page.
// Forwards the user's access token to platform-api /me/partner/users —
// that endpoint resolves the partner from the caller's User.partnerId, so
// no partner slug needs to leak into the URL. Returns 403 if the caller
// isn't partner staff.

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}/me/partner/users`, {
      headers: { Authorization: `Bearer ${accessToken}` },
    })
  } catch (err: unknown) {
    const e = err as { statusCode?: number; data?: unknown }
    throw createError({ statusCode: e.statusCode ?? 500, data: e.data })
  }
})
