// src/modules/ai-chat/aiChat.routes.ts
import { Router } from 'express';
import { AskChatService } from './ask-chat-service';
import { AskChatController } from './ask-chat-controller';
import path from 'node:path';

export const createAskChatRouter = () => {
  const router = Router();

  const indexPath =
    process.env.INDEX_PATH ?? path.join(__dirname, 'index.json');

  const service = new AskChatService({
    // indexPath,
    ollamaUrl: process.env.OLLAMA_URL ?? 'http://localhost:11434',
    embedModel: process.env.EMBED_OLLAMA_MODEL ?? 'nomic-embed-text',
    chatModel: process.env.DEFAULT_OLLAMA_MODEL ?? 'llama3.2:1b',
    topK: Number(process.env.TOP_K ?? 6),
    requestTimeoutMs: Number(process.env.OLLAMA_TIMEOUT_MS ?? 120_000),
  });
  const controller = new AskChatController(service);

  router.get('/health', controller.health);
  router.post('/ask', controller.ask);

  // optional (disabled unless ALLOW_INDEX_RELOAD=1)
  router.post('/reload-index', controller.reloadIndex);

  return router;
};
