import winston, { format, transports } from "winston"; // Use ES6 import
// No longer needed: import { TransformableInfo } from 'logform';

// Define the type for the log info object after timestamp is added
// We can simplify this for now or try to infer from winston later
// type TimestampedLogInfo = TransformableInfo & {
//     timestamp: string;
// };

const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || "info", // Use LOG_LEVEL from env or default to 'info'
  format: format.combine(
    format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), // This adds the timestamp
    format.printf((info: any) => {
      // Use 'any' for now to bypass strict type checking here
      // Ensure message exists, handle potential non-string messages if necessary
      // The 'info' object structure depends on the preceding formatters
      const timestamp = info.timestamp || new Date().toISOString(); // Fallback if timestamp isn't added
      const level = (info.level || "info").toUpperCase();
      const message =
        typeof info.message === "string" ? info.message : JSON.stringify(info.message);
      return `${timestamp} ${level}: ${message}`;
    }),
  ),
  transports: [
    new transports.Console(),
    // Optionally add file transport
    // new transports.File({ filename: 'combined.log' }),
    // new transports.File({ filename: 'error.log', level: 'error' }),
  ],
});

export default logger; // Use ES6 export default
