// POST /api/printers/select - Seleccionar la impresora activa
import { setSelectedPrinter, getPrinterById } from '../../utils/printers'

export default defineEventHandler(async (event) => {
  try {
    const body = await readBody(event)
    const { printerId } = body as { printerId: string | null }

    if (printerId) {
      const printer = await getPrinterById(printerId)
      if (!printer) {
        return {
          ok: false,
          error: 'Impresora no encontrada'
        }
      }
    }

    await setSelectedPrinter(printerId)

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