// P01 — Conversational E-commerce Assistant
// Terminal install demo (using real commands from the repo's pyproject.toml,
// docker-compose.yml, and quickstart in 01-ecommerce-assistant/README.md).
// Duration: ~92s.

const P01_TERMINAL_SCRIPT = [
  { t: 0.3,  kind: 'caption', text: '01 / Clone the portfolio repo' },
  { t: 0.8,  kind: 'cmd', text: 'git clone git@github.com:Juadsuarezsan/conversational-ecommerce-assistant.git', cwd: '~' },
  { t: 2.6,  kind: 'out', text: 'Cloning into \'conversational-ecommerce-assistant\'...' },
  { t: 2.9,  kind: 'out', text: 'remote: Enumerating objects: 1247, done.' },
  { t: 3.1,  kind: 'out', text: 'remote: Counting objects: 100% (412/412), done.' },
  { t: 3.55, kind: 'out', text: 'Receiving objects: 100% (1247/1247), 4.18 MiB | 12.4 MiB/s, done.' },
  { t: 3.75, kind: 'out', text: 'Resolving deltas: 100% (684/684), done.' },
  { t: 4.0,  kind: 'cmd', text: 'cd conversational-ecommerce-assistant', cwd: '~' },

  { t: 5.0,  kind: 'caption', text: '02 / Configure environment variables' },
  { t: 5.5,  kind: 'cmd', text: 'cp .env.example .env' },
  { t: 6.6,  kind: 'cmd', text: '$EDITOR .env' },
  { t: 7.8,  kind: 'editor', title: '.env',
    lines: [
      "ANTHROPIC_API_KEY=sk-ant-•••••••••••••••••",
      "ANTHROPIC_MODEL=claude-sonnet-4-5",
      "",
      "# Embeddings — paid or local fallback",
      "VOYAGE_API_KEY=pa-••••••••••",
      "EMBEDDING_BACKEND=voyage      # local | voyage",
      "",
      "# Reranking — paid or local fallback",
      "COHERE_API_KEY=••••••••••",
      "RERANK_BACKEND=cohere         # local | cohere",
      "",
      "# Infrastructure",
      "DATABASE_URL=postgresql://ecommerce:dev@localhost:5432/ecommerce",
      "QDRANT_URL=http://localhost:6333",
      "REDIS_URL=redis://localhost:6379/0",
      "VECTOR_STORE=qdrant           # qdrant | pgvector | chroma",
    ]},
  { t: 10.6, kind: 'out', text: '✓ .env saved (11 variables, no secrets staged)', color: 'success' },

  { t: 11.2, kind: 'caption', text: '03 / Bring up the stack (postgres + qdrant + redis + api + streamlit)' },
  { t: 11.6, kind: 'cmd', text: 'docker compose up -d' },
  { t: 12.4, kind: 'out', text: '[+] Running 6/6' },
  { t: 12.7, kind: 'out', text: ' ✔ Network 01-ecommerce-assistant_default       Created       0.1s', color: 'success' },
  { t: 13.1, kind: 'out', text: ' ✔ Container ecommerce-postgres-1  (pgvector/pgvector:pg16)  Started  1.4s', color: 'success' },
  { t: 13.6, kind: 'out', text: ' ✔ Container ecommerce-qdrant-1    (qdrant/qdrant:v1.12.0)   Started  2.0s', color: 'success' },
  { t: 14.0, kind: 'out', text: ' ✔ Container ecommerce-redis-1     (redis:7-alpine)          Started  2.4s', color: 'success' },
  { t: 14.5, kind: 'out', text: ' ✔ Container ecommerce-api-1       (FastAPI :8000)           Started  3.2s', color: 'success' },
  { t: 15.0, kind: 'out', text: ' ✔ Container ecommerce-streamlit-1 (Streamlit :8501)         Started  3.6s', color: 'success' },
  { t: 15.6, kind: 'cmd', text: 'docker compose ps' },
  { t: 16.4, kind: 'table', cols: ['NAME', 'IMAGE', 'STATUS'], rows: [
    ['ecommerce-postgres-1',   'pgvector/pgvector:pg16',  'Up (healthy)'],
    ['ecommerce-qdrant-1',     'qdrant/qdrant:v1.12.0',   'Up (healthy)'],
    ['ecommerce-redis-1',      'redis:7-alpine',          'Up (healthy)'],
    ['ecommerce-api-1',        'ecommerce-assistant',     'Up'],
    ['ecommerce-streamlit-1',  'ecommerce-assistant',     'Up'],
  ]},

  { t: 19.4, kind: 'caption', text: '04 / Install Python dependencies (uv)' },
  { t: 19.8, kind: 'cmd', text: 'uv venv && source .venv/bin/activate' },
  { t: 20.6, kind: 'out', text: 'Using CPython 3.11.9' },
  { t: 20.85, kind: 'out', text: 'Creating virtual environment at: .venv' },
  { t: 21.2, kind: 'cmd', text: 'uv pip install -e ".[dev]"' },
  { t: 22.0, kind: 'out', text: 'Resolved 142 packages in 318ms' },
  { t: 22.4, kind: 'progress', start: 22.4, end: 25.6, total: 142, unit: 'packages', label: 'Installing' },
  { t: 25.7, kind: 'out', text: 'Installed 142 packages in 3.18s', color: 'success' },
  { t: 26.0, kind: 'out', text: '  + anthropic==0.39.0  langgraph==0.2.45  voyageai==0.3.2', color: 'accent' },
  { t: 26.2, kind: 'out', text: '  + qdrant-client==1.12.0  rank-bm25==0.2.2  ragas==0.2.6', color: 'accent' },

  { t: 26.8, kind: 'caption', text: '05 / Download the Instacart catalog from Kaggle' },
  { t: 27.0, kind: 'cmd', text: 'python scripts/download_instacart.py' },
  { t: 28.1, kind: 'out', text: '→ Authenticating with Kaggle API...' },
  { t: 28.6, kind: 'out', text: '→ License: CC BY-NC-SA 4.0 (portfolio use OK)' },
  { t: 29.2, kind: 'out', text: '→ Downloading instacart-market-basket-analysis.zip (208 MB)' },
  { t: 29.4, kind: 'progress', start: 29.4, end: 33.6, total: 208, unit: 'MB',  label: 'Downloading' },
  { t: 33.8, kind: 'out', text: '→ Extracting 6 CSV files to data/raw/...' },
  { t: 34.4, kind: 'out', text: '→ Verifying SHA-256 against data/MANIFEST.txt...' },
  { t: 35.1, kind: 'out', text: '✓ products.csv         49,688 rows', color: 'success' },
  { t: 35.4, kind: 'out', text: '✓ orders.csv        3,421,083 rows', color: 'success' },
  { t: 35.7, kind: 'out', text: '✓ aisles.csv             134 rows', color: 'success' },
  { t: 36.0, kind: 'out', text: '✓ departments.csv         21 rows', color: 'success' },
  { t: 36.3, kind: 'out', text: '✓ Manifest verified — dataset ready', color: 'success' },

  { t: 37.0, kind: 'caption', text: '06 / Index products into Qdrant + pgvector + Chroma (parallel)' },
  { t: 37.4, kind: 'cmd', text: 'python -m src.ingestion.index_products --all-stores' },
  { t: 38.4, kind: 'out', text: 'INFO  [ingestion] Loading products.csv (49,688 rows)' },
  { t: 39.0, kind: 'out', text: 'INFO  [ingestion] Building BM25 index over product_name+aisle+department' },
  { t: 39.6, kind: 'out', text: 'INFO  [ingestion] BM25 vocabulary: 28,704 terms' },
  { t: 40.2, kind: 'out', text: 'INFO  [ingestion] Embedding with voyage-3 (batch_size=128, dim=1024)' },
  { t: 40.5, kind: 'progress', start: 40.5, end: 48.5, total: 49688, unit: 'products', label: 'Embedding' },
  { t: 48.7, kind: 'out', text: 'INFO  [ingestion] Upserted 49,688 vectors → qdrant: products_v1 (HNSW m=16, ef=200)' },
  { t: 49.2, kind: 'out', text: 'INFO  [ingestion] Upserted 49,688 vectors → pgvector: products_v1 (ivfflat lists=100)' },
  { t: 49.7, kind: 'out', text: 'INFO  [ingestion] Upserted 49,688 vectors → chroma: products_v1 (default HNSW)' },
  { t: 50.2, kind: 'out', text: 'INFO  [ingestion] Cost: $0.41 · Wall: 8.1s · Throughput: 6,134 emb/s', color: 'accent' },

  { t: 51.4, kind: 'caption', text: '07 / Run benchmark: 4 retrieval systems × 100 queries' },
  { t: 51.8, kind: 'cmd', text: 'python -m src.eval.benchmark_vectordbs --queries data/eval/queries.jsonl' },
  { t: 53.0, kind: 'out', text: 'INFO  [eval] Loaded 100 queries (30 lookup · 30 semantic · 20 compare · 20 multi-turn)' },
  { t: 53.6, kind: 'out', text: 'INFO  [eval] Running BM25 / dense / hybrid+RRF / hybrid+rerank × {Qdrant, pgvector, Chroma}' },
  { t: 53.9, kind: 'progress', start: 53.9, end: 62.6, total: 1200, unit: 'queries', label: 'Benchmarking' },
  { t: 62.8, kind: 'out', text: 'INFO  [eval] Compiling results...' },
  { t: 63.4, kind: 'table',
    cols: ['system', 'P@5', 'Recall@10', 'nDCG@10', 'p95', '$/1k'],
    rows: [
      ['BM25 only',                  '0.612', '0.704', '0.658', '180ms', '$0.04'],
      ['Dense  (Qdrant + voyage-3)', '0.678', '0.741', '0.712', '240ms', '$0.18'],
      ['Hybrid + RRF (k=60)',        '0.741', '0.812', '0.788', '290ms', '$0.22'],
      ['+ Cohere rerank v3',         '0.823', '0.849', '0.844', '480ms', '$1.21', 'highlight'],
    ]},
  { t: 69.0, kind: 'cmd', text: 'python -m src.eval.ragas_eval' },
  { t: 70.0, kind: 'out', text: 'INFO  [ragas] faithfulness · answer_relevance · context_precision · context_recall' },
  { t: 70.3, kind: 'progress', start: 70.3, end: 76.0, total: 400, unit: 'evals', label: 'RAGAS judging' },
  { t: 76.2, kind: 'out', text: '✓ faithfulness          0.91', color: 'success' },
  { t: 76.4, kind: 'out', text: '✓ answer_relevance      0.88', color: 'success' },
  { t: 76.6, kind: 'out', text: '✓ context_precision     0.85', color: 'success' },
  { t: 76.8, kind: 'out', text: '✓ context_recall        0.81', color: 'success' },
  { t: 77.2, kind: 'out', text: '→ Saved to eval/runs/2026-05-16T14-22-08.json' },
  { t: 77.6, kind: 'out', text: '→ Traces pushed to LangSmith · 100 runs · public', color: 'accent' },

  { t: 78.6, kind: 'caption', text: '08 / Launch the Streamlit demo UI' },
  { t: 79.0, kind: 'cmd', text: 'streamlit run streamlit_app.py' },
  { t: 80.0, kind: 'out', text: '  You can now view your Streamlit app in your browser.' },
  { t: 80.4, kind: 'out', text: '  Local URL:    http://localhost:8501', color: 'accent' },
  { t: 80.7, kind: 'out', text: '' },
  { t: 80.9, kind: 'out', text: '  ✓ Connected to PostgreSQL (ecommerce)         ', color: 'success' },
  { t: 81.2, kind: 'out', text: '  ✓ Connected to Qdrant (products_v1)           ', color: 'success' },
  { t: 81.5, kind: 'out', text: '  ✓ Connected to Redis (session cache)          ', color: 'success' },
  { t: 81.8, kind: 'out', text: '  ✓ LangSmith tracing enabled                    ', color: 'success' },
  { t: 82.1, kind: 'out', text: '  ✓ Health: GET /health → 200 OK · ready in 1.4s', color: 'success' },
  { t: 83.0, kind: 'caption', text: '✓ Stack is live. Ask: "lactose-free breakfast for kids under $10"' },
];

const cwd01 = '~/conversational-ecommerce-assistant';
const P01TerminalDemo = makeTerminalDemo({
  script: P01_TERMINAL_SCRIPT,
  duration: 88,
  persistKey: 'p01-terminal',
  title: 'zsh — conversational-ecommerce-assistant — 132×42',
  cwd: cwd01,
  captionPrefix: 'P01',
});

Object.assign(window, { P01TerminalDemo });
