// POST /api/printers - Crear nueva impresora
import { createPrinter } from '../../utils/printers'

export default defineEventHandler(async (event) => {
  try {
    const body = await readBody(event)

    const { name, host, deviceId, timeout, isDefault } = body as {
      name: string
      host: string
      deviceId: string
      timeout?: number
      isDefault?: boolean
    }

    if (!name || !host || !deviceId) {
      return {
        ok: false,
        error: 'name, host y deviceId son requeridos'
      }
    }

    const printer = await createPrinter({
      name,
      host,
      deviceId,
      timeout,
      isDefault
    })

    return {
      ok: true,
      printer
    }
  } catch (err: any) {
    return {
      ok: false,
      error: err.message
    }
  }
})
