/**
 * Structured logger — pino with JSON output in production, pretty in dev.
 *
 * Use as:
 *   import { log, withRequest } from "@/lib/logger";
 *   log.info({ doc_id, page }, "rendering page");
 *   log.error({ err }, "embed-service down");
 *
 * For request-scoped logging:
 *   const reqLog = withRequest(request);
 *   reqLog.info({ duration_ms: dt }, "hybrid_search done");
 *
 * Edge runtime falls back to a console adapter (pino requires node).
 */
import pino from "pino";

// Edge runtime doesn't support pino's worker thread; detect and fall back.
const isEdge = typeof process === "undefined" || process.env.NEXT_RUNTIME === "edge";

function build(): pino.Logger {
  if (isEdge) {
    // Minimal adapter so middleware can call log.* without crashing.
    const noop = () => undefined;
    return {
      info: (o: unknown, m?: string) => console.log(JSON.stringify({ level: "info", msg: m, ...(typeof o === "object" ? o : { v: o }) })),
      warn: (o: unknown, m?: string) => console.warn(JSON.stringify({ level: "warn", msg: m, ...(typeof o === "object" ? o : { v: o }) })),
      error: (o: unknown, m?: string) => console.error(JSON.stringify({ level: "error", msg: m, ...(typeof o === "object" ? o : { v: o }) })),
      debug: noop,
      trace: noop,
      fatal: (o: unknown, m?: string) => console.error(JSON.stringify({ level: "fatal", msg: m, ...(typeof o === "object" ? o : { v: o }) })),
      child: () => build(),
    } as unknown as pino.Logger;
  }
  return pino({
    level: process.env.LOG_LEVEL || "info",
    base: {
      app: "disclosure-web",
      env: process.env.NODE_ENV || "development",
    },
    timestamp: pino.stdTimeFunctions.isoTime,
    // Production: NDJSON (one JSON per line). Dev: pretty-printed.
    transport: process.env.NODE_ENV === "production" ? undefined : {
      target: "pino-pretty",
      options: { colorize: true, translateTime: "SYS:HH:MM:ss.l" },
    },
  });
}

export const log: pino.Logger = build();

/** Create a child logger bound to a request's correlation id. */
export function withRequest(req: Request | { headers: Headers }): pino.Logger {
  const id = req.headers.get("x-correlation-id") ||
             req.headers.get("x-request-id") ||
             cryptoRandomId();
  return log.child({ correlation_id: id });
}

/** Get-or-mint a correlation id for a request. */
export function correlationId(req: Request | { headers: Headers }): string {
  return req.headers.get("x-correlation-id") ||
         req.headers.get("x-request-id") ||
         cryptoRandomId();
}

function cryptoRandomId(): string {
  // 16 hex chars — short enough for logs, enough entropy for non-security uses.
  // Both edge runtime and Node 19+ expose globalThis.crypto; older Node falls
  // back to Math.random (acceptable: this is correlation, not security).
  const g = globalThis as { crypto?: { getRandomValues?: (a: Uint8Array) => void } };
  if (g.crypto?.getRandomValues) {
    const buf = new Uint8Array(8);
    g.crypto.getRandomValues(buf);
    return Array.from(buf, (b) => b.toString(16).padStart(2, "0")).join("");
  }
  return Math.random().toString(36).slice(2, 18);
}
