const cors = require('cors');

const corsMiddleware = cors({
    origin: function (origin, callback) {
        // Allow all origins
        callback(null, true);
    },
    methods: process.env.CORS_METHODS?.split(',') || ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    credentials: process.env.CORS_CREDENTIALS === 'true' || true,
    allowedHeaders: [
        'Content-Type', 
        'Authorization', 
        'X-Requested-With',
        'Origin',
        'X-Gateway-Request-ID',
        'X-Gateway-Timestamp',
        'X-Forwarded-By',
        'X-Forwarded-For',
        'X-Forwarded-Proto',
        'X-Forwarded-Host',
        'X-Session-Token',
        'X-Platform',
        'X-App-Version'
    ],
    exposedHeaders: [
        'Content-Length', 
        'X-Total-Count',
        'X-Gateway-Request-ID',
        'X-Gateway-Timestamp',
        'X-Forwarded-By',
        'X-Forwarded-For',
        'X-Forwarded-Proto',
        'X-Forwarded-Host'
    ],
    maxAge: 86400 // 24 hours
});

module.exports = corsMiddleware;