// Get customer code for an existing Helcim customer
import { requireAuth } from '../../utils/auth.js'
import { getHelcimCustomer } from '../../utils/helcim.js'

export default defineEventHandler(async (event) => {
  try {
    const member = await requireAuth(event)

    if (!member.helcimCustomerId) {
      throw createError({
        statusCode: 400,
        statusMessage: 'No Helcim customer ID found'
      })
    }

    const customerData = await getHelcimCustomer(member.helcimCustomerId)

    return {
      success: true,
      customerId: customerData.id,
      customerCode: customerData.customerCode
    }

  } catch (error) {
    if (error.statusCode) throw error
    console.error('Error getting customer code:', error)
    throw createError({
      statusCode: 500,
      statusMessage: 'An unexpected error occurred'
    })
  }
})
