import pino from "pino";

const isProduction = process.env["NODE_ENV"] === "production";

const LOG_LEVEL = process.env["LOG_LEVEL"] ?? "info";
const devDestination = pino.destination({ dest: 1, sync: true });

const REDACT_PATHS = [
  "password",
  "*.password",
  "*.*.password",
  "newPassword",
  "*.newPassword",
  "currentPassword",
  "*.currentPassword",
  "passwordHash",
  "*.passwordHash",
  "token",
  "*.token",
  "*.*.token",
  "accessToken",
  "*.accessToken",
  "refreshToken",
  "*.refreshToken",
  "apiKey",
  "*.apiKey",
  "authorization",
  "*.authorization",
  "cookie",
  "*.cookie",
  "totp",
  "*.totp",
  "totpSecret",
  "*.totpSecret",
  "secret",
  "*.secret",
  "req.headers.authorization",
  "req.headers.cookie",
  'res.headers["set-cookie"]',
];

const redactConfig = { paths: REDACT_PATHS, censor: "[REDACTED]" };

export const logger = isProduction
  ? pino({
      level: LOG_LEVEL,
      base: { service: "nexus-api" },
      redact: redactConfig,
    })
  : pino(
      {
        level: LOG_LEVEL,
        base: { service: "nexus-api" },
        redact: redactConfig,
        formatters: {
          level(label: string) {
            return { level: label };
          },
        },
      },
      devDestination,
    );

export type Logger = typeof logger;
