/**
 * GFS RAG Document Ingestion
 * 
 * Loads business documents (SOPs, standing orders, architecture docs,
 * risk registers, policies) into the vector store for retrieval.
 * 
 * Auto-discovers and ingests all .md files from known locations.
 */

import fs from 'fs';
import path from 'path';
import { RAGDocumentCategory } from './types';
import { chunkDocument, createDocument } from './chunker';
import { vectorStore } from './vector-store';

/** Map of known document paths to their categories */
const DOCUMENT_SOURCES: {
  path: string;
  category: RAGDocumentCategory;
  tags: string[];
}[] = [
  // Standing Orders
  {
    path: 'lib/gfs/supervision/STANDING_ORDERS.md',
    category: 'standing_order',
    tags: ['safety', 'rules', 'mandatory', 'supervision'],
  },
  // SOPs (docs/operations/)
  {
    path: 'docs/operations/GFS_AGENT_SUPERVISION_SOP.md',
    category: 'sop',
    tags: ['supervision', 'sop', 'process', 'risk'],
  },
  // Architecture (docs/architecture/)
  {
    path: 'docs/architecture/dark-factory-standalone-architecture.md',
    category: 'architecture',
    tags: ['dark-factory', 'architecture', 'technical'],
  },
  {
    path: 'docs/architecture/dark-factory-enhancement-plan.md',
    category: 'architecture',
    tags: ['dark-factory', 'roadmap', 'enhancement'],
  },
  {
    path: 'docs/architecture/llm-routing-cheatsheet.md',
    category: 'architecture',
    tags: ['llm', 'routing', 'model-selection'],
  },
  {
    path: 'docs/architecture/proprietary-llm-exploration.md',
    category: 'architecture',
    tags: ['llm', 'proprietary', 'research'],
  },
  // Business Model (docs/marketing/)
  {
    path: 'docs/marketing/dark-factory-marketing-highlights.md',
    category: 'business_model',
    tags: ['marketing', 'messaging', 'sales'],
  },
  // Agent Guides
  {
    path: 'lib/gfs/README.md',
    category: 'agent_guide',
    tags: ['gfs', 'architecture', 'agent', 'overview'],
  },
  {
    path: 'lib/dark-factory/README.md',
    category: 'agent_guide',
    tags: ['dark-factory', 'code-generation', 'overview'],
  },
  // Public Docs
  {
    path: 'public/docs/module-configuration-guide.md',
    category: 'product_spec',
    tags: ['modules', 'configuration', 'customer'],
  },
  {
    path: 'public/docs/agent-hierarchy-guide.md',
    category: 'product_spec',
    tags: ['agents', 'hierarchy', 'roles'],
  },
];

let _ingested = false;
let _ingestError: string | null = null;

/**
 * Ingest all known business documents into the vector store.
 * Idempotent — only runs once per process lifecycle.
 */
export function ingestBusinessDocuments(): {
  success: boolean;
  documentsIngested: number;
  chunksCreated: number;
  errors: string[];
} {
  if (_ingested) {
    const stats = vectorStore.getStats();
    return {
      success: true,
      documentsIngested: stats.documents,
      chunksCreated: stats.totalChunks,
      errors: _ingestError ? [_ingestError] : [],
    };
  }

  const cwd = process.cwd();
  let documentsIngested = 0;
  let chunksCreated = 0;
  const errors: string[] = [];

  for (const source of DOCUMENT_SOURCES) {
    const fullPath = path.join(cwd, source.path);
    try {
      if (!fs.existsSync(fullPath)) {
        // Not an error — some docs may not exist yet
        continue;
      }

      const content = fs.readFileSync(fullPath, 'utf-8');
      if (!content.trim()) continue;

      // Extract title from first heading or filename
      const titleMatch = content.match(/^#\s+(.+)$/m);
      const title = titleMatch
        ? titleMatch[1].trim()
        : path.basename(source.path, '.md');

      const doc = createDocument(
        source.path,
        title,
        content,
        source.category,
        source.tags
      );

      const chunks = chunkDocument(doc);
      vectorStore.addChunks(chunks);

      documentsIngested++;
      chunksCreated += chunks.length;

      console.log(
        `[RAG-Ingest] ${source.path}: ${chunks.length} chunks (${source.category})`
      );
    } catch (error) {
      const msg = `Failed to ingest ${source.path}: ${
        error instanceof Error ? error.message : 'Unknown error'
      }`;
      errors.push(msg);
      console.error(`[RAG-Ingest] ${msg}`);
    }
  }

  _ingested = true;
  if (errors.length > 0) {
    _ingestError = errors.join('; ');
  }

  console.log(
    `[RAG-Ingest] Complete: ${documentsIngested} documents, ${chunksCreated} chunks`
  );

  return { success: errors.length === 0, documentsIngested, chunksCreated, errors };
}

/**
 * Force re-ingestion (use when documents change).
 */
export function reingestDocuments(): ReturnType<typeof ingestBusinessDocuments> {
  _ingested = false;
  _ingestError = null;
  vectorStore.clear();
  return ingestBusinessDocuments();
}
