import { apiRoute } from '@/lib/api/handler'
import { ApiError, ERROR_CODES } from '@/lib/api/errors'
import { syncConnector } from '@/lib/services/integration-service'

export const dynamic = 'force-dynamic'

/**
 * What this source is currently sending, mapped.
 *
 * Runs a pass without persisting it: a preview that wrote health samples would move the
 * connector's history every time somebody looked at it.
 */
export const GET = apiRoute<{ code: string }>(
  { permission: 'integrations.read', limit: 60 },
  async ({ params }) => {
    const result = await syncConnector({ code: params.code, batchSize: 12, persist: false })
    if (!result) throw new ApiError(ERROR_CODES.notFound)

    return {
      code: result.code,
      mode: result.mode,
      label: result.label,
      recordsIn: result.recordsIn,
      accepted: result.accepted,
      rejected: result.rejected,
      rejectionReasons: result.rejectionReasons,
      preview: result.preview,
      pipeline: result.pipeline,
      quality: result.quality,
    }
  },
)
