/**
 * This script processes the raw step verification data and creates simplified
 * fixture files for easier test comparison
 */

const fs = require('fs');
const path = require('path');

const stepDir = path.join(__dirname, '../../test-results/step-verification');
const fixturesDir = path.join(__dirname, 'legacy-sequences');

function processStepData(stepNumber, stepName) {
  const stepPath = path.join(stepDir, `step-${stepNumber}`);
  const apiCallsFile = path.join(stepPath, 'api-calls.json');

  if (!fs.existsSync(apiCallsFile)) {
    console.log(`⚠️  Step ${stepNumber} API calls not found`);
    return;
  }

  const apiCalls = JSON.parse(fs.readFileSync(apiCallsFile, 'utf8'));

  // Filter to just requests
  const requests = apiCalls.filter(call => call.type === 'request');

  // Create simplified version with just essential info
  const simplified = requests.map((call, index) => {
    const url = new URL(call.url);
    return {
      sequence: index,
      method: call.method,
      url: call.url,
      pathname: url.pathname,
      timestamp: call.timestamp,
      endpoint: `${call.method} ${url.pathname}`,
    };
  });

  // Save simplified version
  const outputFile = path.join(fixturesDir, `${stepName}-simplified.json`);
  fs.writeFileSync(outputFile, JSON.stringify(simplified, null, 2));

  console.log(`✓ Created ${outputFile}`);
  console.log(`  ${simplified.length} API calls`);

  return simplified;
}

console.log('Creating simplified fixtures from step verification data...\n');

// Process each step
const steps = [
  { number: 1, name: 'login' },
  { number: 2, name: 'dashboard' },
  { number: 3, name: 'skip-modal' },
  { number: 4, name: 'navigate-to-session' },
  { number: 5, name: 'session-creation' },
];

const allSequences = {};

for (const step of steps) {
  const sequence = processStepData(step.number, step.name);
  if (sequence) {
    allSequences[step.name] = sequence;
  }
}

// Create combined complete flow
const completeFlow = Object.values(allSequences).flat();
const completeFile = path.join(fixturesDir, 'complete-flow-simplified.json');
fs.writeFileSync(completeFile, JSON.stringify(completeFlow, null, 2));
console.log(`\n✓ Created ${completeFile}`);
console.log(`  ${completeFlow.length} total API calls`);

// Create summary file with unique endpoints
const uniqueEndpoints = new Map();
for (const call of completeFlow) {
  const endpoint = call.endpoint;
  if (!uniqueEndpoints.has(endpoint)) {
    uniqueEndpoints.set(endpoint, 0);
  }
  uniqueEndpoints.set(endpoint, uniqueEndpoints.get(endpoint) + 1);
}

const summary = {
  totalCalls: completeFlow.length,
  uniqueEndpoints: uniqueEndpoints.size,
  endpoints: Array.from(uniqueEndpoints.entries()).map(([endpoint, count]) => ({
    endpoint,
    count,
  })),
  steps: Object.entries(allSequences).map(([name, calls]) => ({
    step: name,
    callCount: calls.length,
  })),
};

const summaryFile = path.join(fixturesDir, 'summary.json');
fs.writeFileSync(summaryFile, JSON.stringify(summary, null, 2));
console.log(`\n✓ Created ${summaryFile}`);
console.log(`  ${summary.uniqueEndpoints} unique endpoints`);
console.log(`  ${summary.totalCalls} total API calls`);
