import pino from "pino";
import { config } from "./config.js";

const LOG_LEVEL = config.logLevel;
const IS_DEV = !config.isProduction;

/**
 * Base pino logger instance.
 * - JSON output in production for machine parsing.
 * - Pretty-printed output in development for readability.
 * - Level controlled by ZENCODE_LOG_LEVEL env var (default: "info").
 */
export const logger = pino({
  level: LOG_LEVEL,
  ...(IS_DEV
    ? {
        transport: {
          target: "pino-pretty",
          options: {
            colorize: true,
            ignore: "pid,hostname",
            translateTime: "HH:MM:ss",
          },
        },
      }
    : {}),
});

/** Create a component-scoped child logger. */
export function createLogger(component: string): pino.Logger {
  return logger.child({ component });
}

/** Create a session-scoped child logger with component and sessionId. */
export function createSessionLogger(
  component: string,
  sessionId: string,
): pino.Logger {
  return logger.child({ component, sessionId });
}
