#!/usr/bin/env node
'use strict';
/**
 * Training Data Harvester — collects training pairs from all sources.
 *
 * Runs incrementally by default (only new data since last harvest).
 * Outputs stats to stdout as JSON for the task runner.
 */
const path = require('path');
const brain = require(path.resolve(__dirname, '..', '..', '..', 'brain'));
const { runHarvest } = require(path.resolve(__dirname, '..', '..', '..', 'training', 'harvester'));

brain.initDb();

async function main() {
  const dataDir = brain.DATA_DIR;
  const result = await runHarvest({ incremental: true, brain, dataDir });

  const summary = [
    `# Training Harvest Complete`,
    '',
    `**Total samples:** ${result.stats.total}`,
    '',
    '## By Source',
    ...Object.entries(result.stats.bySource).map(([k, v]) => `- ${k}: ${v}`),
    '',
    '## By Task Type',
    ...Object.entries(result.stats.byTaskType).map(([k, v]) => `- ${k}: ${v}`),
  ];

  if (result.stats.total === 0) {
    summary.push('', '_No new training data found since last harvest._');
  }

  console.log(summary.join('\n'));
}

// Export for skill-planner; also run directly when invoked as CLI script
module.exports = { run: main };

if (require.main === module) {
  main().catch(err => {
    console.error('[training-harvester] Error:', err.message);
    process.exit(1);
  });
}
