import { countAvailableMitrasFromCache } from '../../services/mitra-status.service.js'

/**
 * Public bestie-availability beacon.
 *
 *   GET /api/public/bestie/available  →  200 { available: bool }
 *
 * Unauthenticated by design: the SHome1st CTA must reflect global availability
 * BEFORE the user has any JWT (see `requirement/flow_customer.mermaid.md` §1 +
 * router.dart's "fresh / unauthenticated users land on Home directly" carve-out).
 *
 * Output is intentionally a single boolean — no `count`, no IDs, no metadata —
 * so this endpoint leaks no operational signal beyond "at least one bestie is
 * online right now". Backed by the same 10s in-memory cache that bounds DB
 * load regardless of poller count.
 *
 * The auth'd `/api/client/mitra-availability` route is kept for CC/debug
 * callers that need the raw count.
 */
export const publicBestieAvailabilityRoutes = async (app) => {
  app.get('/available', async (_request, reply) => {
    const { available } = await countAvailableMitrasFromCache()
    return reply.send({ success: true, data: { available } })
  })
}
