/**
 * PayPlus payment gateway adapter — payment-gateway-adapters (wave-10 leaf 5).
 *
 * PayPlus (https://payplus.co.il/) is an Israeli payment service provider.
 * Generates a payment link for invoice collection.
 *
 * STUB — FAIL-CLOSED: not yet implemented. generatePaymentLink THROWS so no
 * route can serve a real-looking mock URL to a paying customer. Replace the
 * internals with the real PayPlus Payment Pages call (commented below) when
 * credentials are available; remove the throw only together with that work.
 * Spec contract (2026-05-31-payment-gateway-adapters.md §"only built gateways"):
 * a stub adapter must not be reachable in production — this throw enforces it.
 *
 * ACCEPTED-RISK (S9-i2-004): No inbound webhook verification or settlement path
 * exists while this adapter is a stub. Before production use, add signed webhook
 * routes, provider re-fetch confirmation, and idempotent recordInvoicePayment.
 *
 * PayPlus API docs: https://developers.payplus.co.il/
 * Required config: apiKey, secretKey
 */

export interface PayPlusConfig {
  apiKey: string
  secretKey: string
}

export interface PaymentLinkResult {
  url: string
  expiresAt: string | null
}

/**
 * Generate a PayPlus payment link for an invoice.
 *
 * STUB — FAIL-CLOSED: throws until the real PayPlus Payment Pages call replaces it.
 */
export async function generatePaymentLink(
  config: PayPlusConfig,
  invoice: {
    id: string
    invoiceNumber: string | null
    amount: string
    currency: string
    customerName: string
    customerEmail?: string
    description?: string
  },
): Promise<PaymentLinkResult> {
  console.info('[payplus] Generating payment link (stub)', {
    invoiceId: invoice.id,
    invoiceNumber: invoice.invoiceNumber,
    amount: invoice.amount,
    currency: invoice.currency,
    // Never log secretKey
  })

  /*
   * Real PayPlus API call:
   *
   * const res = await fetch('https://restapi.payplus.co.il/api/v1.0/PaymentPages/generateLink', {
   *   method: 'POST',
   *   headers: {
   *     'Authorization': JSON.stringify({ api_key: config.apiKey, secret_key: config.secretKey }),
   *     'Content-Type': 'application/json',
   *   },
   *   body: JSON.stringify({
   *     payment_page_uid: crypto.randomUUID(),
   *     charge_default: {
   *       charge_type: 'regular',
   *       currency_code: invoice.currency,
   *       amount: parseFloat(invoice.amount),
   *     },
   *     customer: {
   *       customer_name: invoice.customerName,
   *       email: invoice.customerEmail,
   *     },
   *     sendEmailApproval: false,
   *     more_info: invoice.id,
   *   }),
   * })
   * const data = await res.json()
   * if (data.results?.status !== '1') throw new Error(`PayPlus error: ${data.results?.description}`)
   * return { url: data.data.payment_page_link, expiresAt: null }
   */

  // FAIL-CLOSED: the PayPlus adapter is a stub with no real API call. Returning a
  // mock `payments.payplus.co.il/mock/...` URL would hand a paying customer a
  // plausible but fake payment link. Throw instead — both live routes (invoices/
  // payment-link and portal/invoices) wrap this in try/catch and surface a 502.
  throw new Error(
    'PayPlus payment gateway is not configured for production. ' +
      'No live PayPlus integration is implemented; refusing to issue a mock payment link.',
  )
}
