'use strict';

/**
 * Plain text adapter.
 * Ingests .txt and .md files with paragraph-aware chunking.
 * Usage: Place files in ~/.walle/imports/text/
 */

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

const IMPORT_DIR = path.join(process.env.HOME, '.walle', 'imports', 'text');
const MAX_CHUNK_SIZE = 800;
const OVERLAP = 100;

function poll(since) {
  const memories = [];
  if (!fs.existsSync(IMPORT_DIR)) return memories;

  const files = fs.readdirSync(IMPORT_DIR).filter(f =>
    f.endsWith('.txt') || f.endsWith('.md')
  );

  for (const file of files) {
    try {
      const filePath = path.join(IMPORT_DIR, file);
      const stat = fs.statSync(filePath);
      const mtime = stat.mtime.toISOString();
      if (since && mtime <= since) continue;

      const content = fs.readFileSync(filePath, 'utf8');
      if (content.length < 50) continue;

      // Chunk by paragraphs
      const chunks = chunkText(content, MAX_CHUNK_SIZE, OVERLAP);

      for (let i = 0; i < chunks.length; i++) {
        memories.push({
          source: 'plaintext',
          source_id: `plaintext-${file}-chunk-${i}`,
          content: chunks[i],
          timestamp: mtime,
          metadata: JSON.stringify({ file, chunk: i, total_chunks: chunks.length }),
        });
      }
    } catch (err) {
      console.error(`[plaintext-adapter] Failed to read ${file}:`, err.message);
    }
  }
  return memories;
}

function chunkText(text, maxSize, overlap) {
  const chunks = [];
  let pos = 0;

  while (pos < text.length) {
    let end = Math.min(pos + maxSize, text.length);

    // Try to break at paragraph boundary
    if (end < text.length) {
      const paraBreak = text.lastIndexOf('\n\n', end);
      if (paraBreak > pos + maxSize * 0.5) end = paraBreak;
      else {
        const lineBreak = text.lastIndexOf('\n', end);
        if (lineBreak > pos + maxSize * 0.5) end = lineBreak;
      }
    }

    const chunk = text.slice(pos, end).trim();
    if (chunk.length >= 50) chunks.push(chunk);

    pos = end - overlap;
    if (pos >= text.length - 50) break; // avoid tiny trailing chunks
  }

  return chunks;
}

module.exports = { poll, chunkText };
