// Utilidades para comunicación con impresoras Epson ePOS
import axios from 'axios'
import https from 'https'

export interface PrinterResponse {
  status: number
  headers: any
  data: string
}

export function buildSoapEnvelope(inner: string): string {
  return (
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' +
    '<s:Body>' +
    '<epos-print xmlns="http://www.epson-pos.com/schemas/2011/03/epos-print">' +
    inner +
    '</epos-print>' +
    '</s:Body>' +
    '</s:Envelope>'
  )
}

export async function sendToPrinter(
  xml: string,
  printerHost: string,
  printerDeviceId: string,
  printerTimeoutMs: number
): Promise<PrinterResponse> {
  const url = `https://${printerHost}/cgi-bin/epos/service.cgi?devid=${encodeURIComponent(printerDeviceId)}&timeout=${printerTimeoutMs}`

  // Agente HTTPS que acepta certificados auto-firmados
  const httpsAgent = new https.Agent({
    rejectUnauthorized: false
  })

  const res = await axios.post(url, xml, {
    headers: {
      'Content-Type': 'text/xml; charset=utf-8'
    },
    httpsAgent,
    timeout: printerTimeoutMs + 5000,
    validateStatus: () => true, // Aceptar cualquier status code
  })

  return {
    status: res.status,
    headers: res.headers,
    data: typeof res.data === 'string' ? res.data : String(res.data)
  }
}

export function parsePrinterResponse(responseData: string): {
  success: boolean
  code: string
} {
  const success = /success\s*=\s*"true"/.test(responseData)
  const codeMatch = responseData.match(/code="([^"]*)"/)
  const code = codeMatch ? codeMatch[1] : ''

  return { success, code }
}
