import { getPostingCategories } from "../../utils/lexofficeClient"

// Powers the "set a booking category?" modal on Lexoffice upload — only "income"
// categories are relevant since this route uploads sales invoices (AR revenue).
export default defineEventHandler(async (event) => {
  try {
    const categories = await getPostingCategories()
    const income = categories
      .filter((c: any) => c?.type === 'income')
      .map((c: any) => ({ id: c.id, name: c.name, groupName: c.groupName }))
    return { status: 200, categories: income }
  } catch (err: any) {
    console.error('[Lexoffice] Failed to fetch posting categories:', err)
    return { status: 500, message: 'Failed to load Lexoffice categories', categories: [] }
  }
})
