const fs = require('fs');
const file = 'c:/Users/kde/Desktop/wassili_V2/routes/orders.js';
let content = fs.readFileSync(file, 'utf8');

const newLimiters = `// Rate Limiter للمفاوضات لمنع الإغراق
const negotiateLimiter = rateLimit({
    windowMs: 5 * 60 * 1000, // 5 minutes
    max: 30, // 30 requests per 5 minutes
    message: { message: 'لقد تجاوزت الحد المسموح به لمحاولات التفاوض. يرجى الانتظار 5 دقائق.' },
    standardHeaders: true,
    legacyHeaders: false,
    validate: { xForwardedForHeader: false, trustProxy: false, ip: false }
});

// Rate Limiter للتقييمات
const ratingLimiter = rateLimit({
    windowMs: 60 * 1000, // 1 minute
    max: 5,
    message: { message: 'يرجى الانتظار قليلاً قبل إرسال تقييم جديد.' },
    standardHeaders: true,
    legacyHeaders: false,
    validate: { xForwardedForHeader: false, trustProxy: false, ip: false }
});`;

// Inject definitions after createOrderLimiter
if (!content.includes('negotiateLimiter = rateLimit')) {
    const findBlock1 = `const createOrderLimiter = rateLimit({
    windowMs: 5 * 60 * 1000, // ✅ 5 minutes
    max: 5, // 5 requests per 5 minutes
    message: { message: 'لقد تجاوزت الحد المسموح به. يرجى الانتظار 5 دقائق.' },
    standardHeaders: true,
    legacyHeaders: false,
    validate: { xForwardedForHeader: false, trustProxy: false, ip: false }
});`;
    
    if (content.includes(findBlock1)) {
        content = content.replace(findBlock1, findBlock1 + '\n\n' + newLimiters);
    }
}

// Inject in negotiate-response
content = content.replace(
    "router.put('/:id/negotiate-response', protect, async (req, res) => {",
    "router.put('/:id/negotiate-response', protect, negotiateLimiter, async (req, res) => {"
);

// Inject in rate
content = content.replace(
    "router.put('/:id/rate', protect, async (req, res) => {",
    "router.put('/:id/rate', protect, ratingLimiter, async (req, res) => {"
);

fs.writeFileSync(file, content);
console.log("Fixed rate limiters in orders.js successfully.");
