const winston = require('winston');
require('winston-daily-rotate-file');
const fs = require('fs');
const path = require('path');

// Define the logs directory path
const logDirectory = path.join(__dirname, '../logs');

// Ensure the logs directory exists
if (!fs.existsSync(logDirectory)) {
    fs.mkdirSync(logDirectory);
}

// Define the log configuration
const logConfiguration = {
    transports: [
        new winston.transports.DailyRotateFile({
            filename: path.join(logDirectory, 'application-%DATE%.log'),
            datePattern: 'YYYY-MM-DD', // new file is created every day
            zippedArchive: true,
            maxSize: '20m',
            maxFiles: '90d',
            level: 'info'
        })
    ],
    format: winston.format.combine(
        winston.format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
    )
};

// Create the logger
const logger = winston.createLogger(logConfiguration);

module.exports = logger;
