const winston = require('winston');
const config = require('../config');

// Create the logger only once
const logger = winston.createLogger({
    level: config.isProduction ? 'info' : 'debug',
    defaultMeta: { service: 'bnkops-map' },
    transports: [
        new winston.transports.Console({
            format: winston.format.combine(
                winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
                winston.format.errors({ stack: true }),
                winston.format.colorize(),
                winston.format.printf(({ timestamp, level, message, service }) => {
                    return `${timestamp} [${service}] ${level}: ${message}`;
                })
            )
        })
    ]
});

// Add file transport in production
if (config.isProduction) {
    logger.add(new winston.transports.File({ 
        filename: 'error.log', 
        level: 'error',
        format: winston.format.combine(
            winston.format.timestamp(),
            winston.format.errors({ stack: true }),
            winston.format.json()
        )
    }));
    logger.add(new winston.transports.File({ 
        filename: 'combined.log',
        format: winston.format.combine(
            winston.format.timestamp(),
            winston.format.errors({ stack: true }),
            winston.format.json()
        )
    }));
}

module.exports = logger;