import { getSubscriptionsByUserId } from '../../database/queries/subscriptions.ts'
import { validateOrganizationAccess } from '../../utils/organizationValidation.ts'
import { handleApiError, ApiErrors, createSuccessResponse } from '../../utils/errors'

export default defineEventHandler(async (event) => {
  try {
    const { organizationId } = getQuery(event)

    if (!organizationId) {
      throw ApiErrors.validationError('Organization ID is required')
    }

    await validateOrganizationAccess(event, organizationId as string)

    const subscriptions = await getSubscriptionsByUserId(organizationId as string)
    return createSuccessResponse(subscriptions, 'Subscriptions retrieved successfully')
  } catch (error) {
    throw handleApiError(error)
  }
})
