import pino from "pino"

/**
 * Isomorphic logger for the API client.
 *
 * - In Node.js (server / Next.js RSC / server actions): pino writes newline-delimited
 *   JSON to stdout — structured and machine-parseable.
 * - In the browser (Next.js client components): pino switches to the browser
 *   transport automatically and delegates to the appropriate `console.*` method
 *   (console.debug, console.warn, console.error, …).
 *
 * Log level defaults:
 *   development  → debug  (all messages)
 *   production   → warn   (warnings + errors only)
 *
 * Override with the NEXT_PUBLIC_LOG_LEVEL env var (e.g. "info").
 */
const level =
    (process.env.NEXT_PUBLIC_LOG_LEVEL as pino.Level | undefined) ??
    (process.env.NODE_ENV === "production" ? "warn" : "debug")

export const logger = pino({
    name: "garage-api",
    level,
    browser: {
        /**
         * Pass a plain object to `console.*` so DevTools display
         * log fields (method, endpoint, status, duration) as expandable props.
         */
        asObject: true,
    },
})
