// PUT /api/printers/:id - Actualizar una impresora
import { updatePrinter } from '../../utils/printers'

export default defineEventHandler(async (event) => {
  try {
    const id = getRouterParam(event, 'id')

    if (!id) {
      return {
        ok: false,
        error: 'ID requerido'
      }
    }

    const body = await readBody(event)

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

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

    if (!printer) {
      return {
        ok: false,
        error: 'Impresora no encontrada'
      }
    }

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