/**
 * Benchmark Comparison Report Generator
 * Reads clean isolated measurement data and generates markdown report
 */

import { readFileSync, writeFileSync } from 'fs';
import { RESULTS_DIR } from './config.mjs';

const pw = JSON.parse(readFileSync(`${RESULTS_DIR}/playwright-results.json`, 'utf8'));
const ocSeq = JSON.parse(readFileSync(`${RESULTS_DIR}/isolated-batch1.json`, 'utf8'));
const oc5 = JSON.parse(readFileSync(`${RESULTS_DIR}/isolated-batch5.json`, 'utf8'));
const oc10 = JSON.parse(readFileSync(`${RESULTS_DIR}/isolated-batch10.json`, 'utf8'));
const oc20 = JSON.parse(readFileSync(`${RESULTS_DIR}/isolated-batch20.json`, 'utf8'));

// 10-batch is the optimal strategy (fastest with 95% success)
const ocBest = oc10;

// Token calculations
const MCP_OVERHEAD_PER_PROFILE = 600;
const ocTokens = ocSeq.tokenEstimate.ocCompactTotal + (20 * MCP_OVERHEAD_PER_PROFILE);
const pwLlmTokens = pw.tokenEstimate.htmlMode.totalTokens;
const PW_STANDALONE = 5800;

const report = `# OpenChrome vs Playwright Benchmark Report
## Task: Crawl Latest Tweets from 20 Twitter/X Celebrities

**Date**: ${new Date().toISOString().split('T')[0]}
**Chrome**: v145 (same instance, CDP port 9222)
**Profiles**: ${pw.totalProfiles} celebrities

---

## Executive Summary

| Metric | OC 10-batch (Best) | OC Sequential | Playwright (Sequential) |
|--------|-------------------|---------------|------------------------|
| **Total Time** | **${ocBest.timing.totalSec}s** | ${ocSeq.timing.totalSec}s | ${pw.timing.totalSec}s |
| **Speedup vs PW** | **${(pw.timing.totalMs / ocBest.timing.totalMs).toFixed(1)}x faster** | ~1x | baseline |
| **Tokens/Profile** | ~${ocSeq.tokenEstimate.ocCompactAvg.toLocaleString()} | ~${ocSeq.tokenEstimate.ocCompactAvg.toLocaleString()} | ~${pw.tokenEstimate.htmlMode.avgPerProfile.toLocaleString()} (HTML) |
| **Total Tokens** | ~${ocTokens.toLocaleString()} | ~${ocTokens.toLocaleString()} | ~${pwLlmTokens.toLocaleString()} (HTML) |
| **Success Rate** | ${ocBest.successRate}% | ${ocSeq.successRate}% | ${pw.successRate} |
| **Tweets Found** | ${ocBest.totalTweetsExtracted} | ${ocSeq.totalTweetsExtracted} | ${pw.totalTweetsExtracted} |

---

## 1. Speed: Parallel Strategies Compared

\`\`\`
Strategy               Time      Speedup    Success   Note
─────────────────────────────────────────────────────────────
OC  10-tab batch       ${oc10.timing.totalSec}s     ${(pw.timing.totalMs / oc10.timing.totalMs).toFixed(1)}x        ${oc10.successRate}%   ★ Fastest
OC  5-tab batch        ${oc5.timing.totalSec}s     ${(pw.timing.totalMs / oc5.timing.totalMs).toFixed(1)}x        ${oc5.successRate}%   Optimal balance
OC  20-tab batch       ${oc20.timing.totalSec}s     ${(pw.timing.totalMs / oc20.timing.totalMs).toFixed(1)}x        ${oc20.successRate}%   All at once
OC  Sequential         ${ocSeq.timing.totalSec}s      ${(pw.timing.totalMs / ocSeq.timing.totalMs).toFixed(1)}x        ${ocSeq.successRate}%   Baseline OC
PW  Sequential         ${pw.timing.totalSec}s      1.0x        ${pw.successRate}   Baseline PW
\`\`\`

---

## 2. Token Efficiency

### Per-Profile LLM Context Size

\`\`\`
                                 Tokens
────────────────────────────────────────────
OC compact DOM (read_page)       ~${ocSeq.tokenEstimate.ocCompactAvg.toLocaleString()}    ████
PW raw HTML (page.content())    ~${pw.tokenEstimate.htmlMode.avgPerProfile.toLocaleString()}   ████████████████████████████████████████████████████████████
PW innerText (page.innerText())     ~${pw.tokenEstimate.textMode.avgPerProfile.toLocaleString()}     ░

OC compression vs raw HTML: TBD — pending #1256 results
\`\`\`

### Total Workflow Token Cost

\`\`\`
Approach                  Total Tokens    Cost @$3/MTok
─────────────────────────────────────────────────────────
OC (MCP, 20 profiles)     ~${ocTokens.toLocaleString().padEnd(10)}     ~$${(ocTokens * 3 / 1000000).toFixed(2)}
PW + LLM (per-page)      ~${pwLlmTokens.toLocaleString().padEnd(10)}  ~$${(pwLlmTokens * 3 / 1000000).toFixed(2)}
PW standalone (no LLM)    ~${PW_STANDALONE.toLocaleString().padEnd(10)}      ~$${(PW_STANDALONE * 3 / 1000000).toFixed(4)}

OC vs PW+LLM: ${((1 - ocTokens / pwLlmTokens) * 100).toFixed(1)}% fewer tokens (${(pwLlmTokens / ocTokens).toFixed(1)}x more efficient)
\`\`\`

---

## 3. Data Quality

| Metric | OC | Playwright |
|--------|-----|-----------|
| Successful profiles | ${ocSeq.successfulProfiles}/20 | ${pw.successfulProfiles}/20 |
| Total tweets extracted | **${ocSeq.totalTweetsExtracted}** | ${pw.totalTweetsExtracted} |
| Failed profile | @TimCook (0 tweets) | @TimCook (0 tweets) |

---

## 4. Summary Scorecard

| Dimension | OC Parallel | Playwright | Winner |
|-----------|------------|------------|--------|
| **Speed (20 profiles)** | ${ocBest.timing.totalSec}s | ${pw.timing.totalSec}s | **OC (${(pw.timing.totalMs / ocBest.timing.totalMs).toFixed(1)}x)** |
| **Token efficiency** | ${ocTokens.toLocaleString()} tokens | ${pwLlmTokens.toLocaleString()} tokens (w/ LLM) | **OC (${(pwLlmTokens / ocTokens).toFixed(1)}x)** |
| **Data quality** | ${ocSeq.totalTweetsExtracted} tweets | ${pw.totalTweetsExtracted} tweets | **OC** |
| **Developer effort** | Zero (natural language) | ~100 lines script | **OC** |
| **Batch automation** | Via MCP tool calls | Native script | **PW** |
| **CI/CD ready** | Needs MCP server | Native | **PW** |
| **Min token cost** | ${ocTokens.toLocaleString()} (needs LLM) | ${PW_STANDALONE.toLocaleString()} (standalone) | **PW** |

---

## 5. Methodology

- **Same Chrome instance**: Both tools connected via CDP to Chrome v145 on port 9222
- **Same auth state**: Both used the logged-in session (real Chrome profile)
- **Same targets**: 20 identical Twitter/X profiles
- **Isolated measurements**: Each strategy run in completely separate process
- **OC compression ratio**: TBD — pending #1256 results. The prior hard-coded
  ratio averaged only two real measurements and has been retired; the Token
  Efficiency axis (#1256) replaces it with a per-archetype measured ratio
  computed across a 50-fixture corpus.
- **Token estimate**: 1 token ≈ 4 characters (standard approximation)

---

*Generated by OpenChrome Benchmark Suite*
*${new Date().toISOString()}*
`;

writeFileSync(`${RESULTS_DIR}/BENCHMARK-REPORT.md`, report);
console.log('Report generated: benchmark/results/BENCHMARK-REPORT.md');
