const fs = require('fs');

const botPath = 'c:/Users/Mobin/Desktop/wizwiz/wizwizxui-timebot/bot.php';
const lines = fs.readFileSync(botPath, 'utf8').split(/\r?\n/);

const start = lines.findIndex((l) => l.includes("preg_match('/^accept"));
const end = lines.findIndex((l, i) => i > start && l.startsWith("if(preg_match('/decline/'"));

if (start < 0 || end < 0) {
  console.error('markers not found', start, end);
  process.exit(1);
}

const block = `if(preg_match('/^accept(.+)$/',$data, $match) and $text != $buttonValues['cancel'] && ($from_id == $admin || $userInfo['isAdmin'] == true)){
    setUser();
    $result = PayApprovalService::approve($match[1]);
    if (!empty($result['alert'])) {
        alert($result['alert']);
    }
    exit();
}`.split('\n');

const out = [...lines.slice(0, start), ...block, ...lines.slice(end)];
fs.writeFileSync(botPath, out.join('\n'), 'utf8');
console.log('accept handler replaced with PayApprovalService, removed', end - start, 'lines');
