import { registerAs } from "@nestjs/config";

/**
 * RabbitMQ connection for the outbox event publisher (payment -> apps).
 * `publisherTransport` selects which PaymentEventPublisher the outbox relay uses:
 *   "rabbitmq" -> RabbitMqPaymentEventPublisher (publish to the payment.events exchange) [DEFAULT]
 *   "http"     -> HttpPaymentEventPublisher (POST to each app's mark-paid endpoint) [rollback]
 * RabbitMQ is the default; set PUBLISHER_TRANSPORT=http explicitly to fall back to HTTP.
 */
export default registerAs("rabbitmq", () => ({
  url: process.env.PAYMENT_RABBITMQ_URL ?? "amqp://localhost:5672/payment",
  publisherTransport: (process.env.PUBLISHER_TRANSPORT ?? "rabbitmq").toLowerCase(),
  /** Publish is failed (so the relay retries) if the broker does not confirm within this. */
  publishTimeoutMs: parseInt(process.env.PAYMENT_PUBLISH_TIMEOUT_MS ?? "10000", 10),
}));

/**
 * True when the outbox relay should publish to RabbitMQ rather than POST over HTTP.
 * RabbitMQ is the default; only an explicit PUBLISHER_TRANSPORT=http opts out.
 */
export function isRabbitPublisher(): boolean {
  return (process.env.PUBLISHER_TRANSPORT ?? "rabbitmq").toLowerCase() !== "http";
}
