// Logging utilities
const fs = require('fs');
const path = require('path');

// Audit logging function
const auditLog = (action, data = {}, req = null) => {
    const timestamp = new Date().toISOString();
    const logEntry = {
        timestamp,
        action,
        data,
        requestId: req?.requestId || 'no-request-id',
        ip: req?.ip || 'unknown',
        userAgent: req?.get('User-Agent') || 'unknown'
    };
    
    const logLine = JSON.stringify(logEntry) + '\n';
    
    // Log to console for development
    console.log(`[AUDIT] ${action}:`, data);
    
    // Log to file
    try {
        fs.appendFileSync(path.join(process.cwd(), 'app.log'), logLine);
    } catch (error) {
        console.error('Failed to write to audit log:', error);
    }
};

// Simple console log for token operations before auditLog is available
const tokenLog = (message, data = {}) => {
    const timestamp = new Date().toISOString();
    console.log(`[TOKEN] ${timestamp} ${message}:`, data);
};

module.exports = {
    auditLog,
    tokenLog
};
