// api/search/indexer.js
const fs = require('fs');
const path = require('path');
const { bm25 } = require('./bm25');

let jieba;
try {
  jieba = require('@node-rs/jieba');
} catch {
  jieba = { cut: (text) => [...new Set(text.replace(/[^一-龥a-zA-Z0-9]/g, ' ').split(/\s+/).filter(t => t.length > 1))] };
}

let index = null;

function loadIndex() {
  if (index) return index;
  const indexPath = path.join(__dirname, '..', '..', 'static', 'doc-index.json');
  if (!fs.existsSync(indexPath)) {
    throw new Error('doc-index.json not found. Run `node scripts/generate-doc-index.js` first.');
  }
  index = JSON.parse(fs.readFileSync(indexPath, 'utf-8'));
  console.log(`Loaded search index: ${index.length} documents`);
  return index;
}

function tokenizeQuery(query) {
  return jieba.cut(query).filter(t => t.length > 1 && !/^[\s\d\W]+$/.test(t));
}

/**
 * Search documents relevant to a query.
 * @param {string} query - user's question
 * @param {number} topK - max results
 * @returns {Array<{title: string, text: string, breadcrumb: string, url: string, score: number}>}
 */
function search(query, topK = 3) {
  const docs = loadIndex();
  const tokens = tokenizeQuery(query);
  if (tokens.length === 0) return [];
  return bm25(tokens, docs, topK);
}

module.exports = { search, loadIndex };
