const fs = require('fs');
const data = JSON.parse(fs.readFileSync('/Users/sergei/Desktop/WellNuo/api/Wellnuo_API.postman_collection.json', 'utf8'));

const endpoints = [];

function extractEndpoints(items) {
  for (const item of items) {
    if (item.item) {
      extractEndpoints(item.item);
    } else if (item.request) {
      const body = item.request.body?.urlencoded || [];
      const funcParam = body.find(p => p.key === 'function');
      const params = body.filter(p => p.key !== 'function').map(p => p.key);

      endpoints.push({
        name: item.name,
        function: funcParam?.value || 'N/A',
        method: item.request.method,
        params: params
      });
    }
  }
}

extractEndpoints(data.item);

console.log('# WellNuo API Endpoints\n');
console.log('| # | Endpoint Name | Function | Parameters |');
console.log('|---|---------------|----------|------------|');

endpoints.forEach((ep, i) => {
  const paramsStr = ep.params.join(', ');
  console.log(`| ${i+1} | ${ep.name} | \`${ep.function}\` | ${paramsStr} |`);
});

console.log(`\n**Всего endpoints: ${endpoints.length}**`);

// Save to file
const output = endpoints.map((ep, i) => ({
  id: i + 1,
  name: ep.name,
  function: ep.function,
  params: ep.params
}));

fs.writeFileSync('/Users/sergei/Desktop/WellNuo/api/endpoints-list.json', JSON.stringify(output, null, 2));
console.log('\nСохранено в endpoints-list.json');
