import { pino, type LogFn, type Logger } from "pino";

export let logLevel = process.env.LOG_LEVEL || "info";

const transport = pino.transport({
    targets: [
        {
            target: "pino-pretty",
            options: {
                colorize: true,
                singleLine: true,
                // customPrettifiers: {
                //     time: (time) => `🕰 ${time}`,
                // },
                destination: process.stdout.fd,
            },
        },
        {
            target: "./dbTransport.js",
        },
        // Only log to Go if LST_USE_GO=true
        ...(process.env.LST_USE_GO === "true"
            ? [
                  {
                      target: "./goTransport.js", // New transport for Go
                  },
              ]
            : []),
    ],
});

const log: Logger = pino(
    {
        level: process.env.LOG_LEVEL || logLevel,
        //level: "debug",
        // formatters: {
        //     level: (label) => {
        //         return {level: label.toUpperCase()};
        //     },
        // },
        //customLevels: {death: 70},
        // removes data from the logs that we dont want to be shown :D
        redact: { paths: ["email", "password"], remove: true },
    },
    transport
);

export const createLog = (
    level: "info" | "fatal" | "error" | "warn" | "debug" | "trace",
    username: string,
    service: string,
    message: string
) => {
    if (level in log) {
        log[level]({ username, service }, message);
    } else {
        log.warn(
            { username, service },
            `Invalid log level '${level}', falling back to warn: ${message}`
        );
    }
};
