import QRCode from 'qrcode'

export interface QrGenerateOptions {
  baseUrl: string
  code: string
  width?: number
  margin?: number
}

/**
 * Generate QR code as data URL (for display)
 */
export async function generateQrDataUrl(opts: QrGenerateOptions): Promise<string> {
  const url = `${opts.baseUrl}/p/${opts.code}`
  return QRCode.toDataURL(url, {
    width: opts.width || 400,
    margin: opts.margin || 2,
    color: { dark: '#1e40af', light: '#ffffff' },
    errorCorrectionLevel: 'M',
  })
}

/**
 * Generate QR code as SVG string (for print)
 */
export async function generateQrSvg(opts: QrGenerateOptions): Promise<string> {
  const url = `${opts.baseUrl}/p/${opts.code}`
  return QRCode.toString(url, {
    type: 'svg',
    width: opts.width || 400,
    margin: opts.margin || 2,
    color: { dark: '#1e40af', light: '#ffffff' },
    errorCorrectionLevel: 'M',
  })
}

/**
 * Generate QR code as PNG buffer (for download)
 */
export async function generateQrBuffer(opts: QrGenerateOptions): Promise<Buffer> {
  const url = `${opts.baseUrl}/p/${opts.code}`
  return QRCode.toBuffer(url, {
    width: opts.width || 800,
    margin: opts.margin || 2,
    color: { dark: '#1e40af', light: '#ffffff' },
    errorCorrectionLevel: 'M',
  })
}
