/**
 * Copyright (c) 2024-2025 Тарабанов Александр Викторович
 * All rights reserved.
 * 
 * This software is proprietary and confidential.
 * Unauthorized copying, modification, or distribution is prohibited.
 * 
 * For licensing inquiries: info@hb3-accelerator.com
 * Website: https://hb3-accelerator.com
 * GitHub: https://github.com/HB3-ACCELERATOR
 */

const axios = require('axios');
const logger = require('../utils/logger');
const ollamaConfig = require('./ollamaConfig');

const VECTOR_SEARCH_URL = process.env.VECTOR_SEARCH_URL || 'http://vector-search:8001';
const TIMEOUTS = ollamaConfig.getTimeouts();

async function upsert(tableId, rows) {
  logger.info(`[VectorSearch] upsert: tableId=${tableId}, rows=${rows.length}`);
  try {
    const res = await axios.post(`${VECTOR_SEARCH_URL}/upsert`, {
      table_id: String(tableId),
      rows: rows.map(r => ({
        row_id: String(r.row_id),
        text: r.text,
        metadata: r.metadata || {}
      }))
    }, {
      timeout: TIMEOUTS.vectorUpsert // Централизованный таймаут для индексации
    });
    logger.info(`[VectorSearch] upsert result:`, res.data);
    return res.data;
  } catch (error) {
    logger.error(`[VectorSearch] upsert error:`, error.message);
    throw error;
  }
}

async function search(tableId, query, topK = 3) {
  logger.info(`[VectorSearch] search: tableId=${tableId}, query="${query}", topK=${topK}`);
  try {
    const res = await axios.post(`${VECTOR_SEARCH_URL}/search`, {
      table_id: String(tableId),
      query,
      top_k: topK
    }, {
      timeout: TIMEOUTS.vectorSearch // Централизованный таймаут для поиска
    });
    logger.info(`[VectorSearch] search result:`, res.data.results);
    return res.data.results;
  } catch (error) {
    logger.error(`[VectorSearch] search error:`, error.message);
    throw error;
  }
}

async function remove(tableId, rowIds) {
  logger.info(`[VectorSearch] remove: tableId=${tableId}, rowIds=${rowIds}`);
  try {
    const res = await axios.post(`${VECTOR_SEARCH_URL}/delete`, {
      table_id: String(tableId),
      row_ids: rowIds.map(String)
    });
    logger.info(`[VectorSearch] remove result:`, res.data);
    return res.data;
  } catch (error) {
    logger.error(`[VectorSearch] remove error:`, error.message);
    throw error;
  }
}

async function rebuild(tableId, rows) {
  logger.info(`[VectorSearch] rebuild: tableId=${tableId}, rows=${rows.length}`);
  try {
    const res = await axios.post(`${VECTOR_SEARCH_URL}/rebuild`, {
      table_id: String(tableId),
      rows: rows.map(r => ({
        row_id: String(r.row_id),
        text: r.text,
        metadata: r.metadata || {}
      }))
    });
    logger.info(`[VectorSearch] rebuild result:`, res.data);
    return res.data;
  } catch (error) {
    logger.error(`[VectorSearch] rebuild error:`, error.message);
    throw error;
  }
}

async function health() {
  logger.info(`[VectorSearch] health check`);
  try {
    const res = await axios.get(`${VECTOR_SEARCH_URL}/health`, { timeout: TIMEOUTS.vectorHealth });
    logger.info(`[VectorSearch] health result:`, res.data);
    return {
      status: 'ok',
      url: VECTOR_SEARCH_URL,
      response: res.data
    };
  } catch (error) {
    logger.error(`[VectorSearch] health error:`, error.message);
    return {
      status: 'error',
      url: VECTOR_SEARCH_URL,
      error: error.message
    };
  }
}

module.exports = {
  upsert,
  search,
  remove,
  rebuild,
  health
}; 