/**
 * A2 — ioredis client + `RedisPublisher` adapter for the P3 server entrypoint.
 *
 * `createRedisClient(url)` constructs the single ioredis connection used for the revocation-bus
 * PUBLISH side (the FROZEN `relay:revocations` channel; P1 nodes subscribe — see routing/bus.ts).
 * `createRedisPublisher` narrows that client to the minimal `RedisPublisher` surface
 * `createRedisRevocationBus` consumes, so the concrete ioredis type never leaks into the bus wiring
 * and the seam stays unit-testable with a fake. INV9: no secrets pass through here — the URL is
 * owned/validated by `loadEnv` and never logged.
 */
// The default export IS `Redis`; imported by name because this package's tsconfig has no
// `esModuleInterop`, under which a default import of the CJS module resolves to its namespace.
import { Redis } from 'ioredis'
import type { RedisPublisher } from '../routing/bus.js'

/**
 * Construct the production ioredis client from a validated connection URL. Connection lifecycle
 * (connect/retry) is managed by ioredis; the caller owns `quit()` on shutdown.
 */
export function createRedisClient(url: string): Redis {
  return new Redis(url)
}

/**
 * Adapt an ioredis client to the `RedisPublisher` seam. Forwards `publish(channel, message)`
 * verbatim and returns the subscriber count the driver reports (Promise<number>).
 */
export function createRedisPublisher(redis: Redis): RedisPublisher {
  return {
    publish: (channel: string, message: string): Promise<number> => redis.publish(channel, message),
  }
}
