import { syncCardsWithDatabase } from '../services/cardSync.service'

export default defineEventHandler(async (event) => {
  const runtimeConfig = useRuntimeConfig()
  const folderPath = runtimeConfig.NUXT_PATH_FILES

  if (!folderPath) {
    throw createError({
      statusCode: 500,
      message: 'NUXT_PATH_FILES not configured'
    })
  }

  try {
    const result = await syncCardsWithDatabase(folderPath)

    return {
      success: true,
      ...result
    }
  } catch (error: any) {
    throw createError({
      statusCode: 500,
      message: error.message
    })
  }
})
