import { Router } from 'express';
import { VectorDatabaseModel } from '../models/VectorDatabaseModel';
import { logger } from '../utils/logger';

const router = Router();

/**
 * GET /api/vector/document-chunks/:documentId
 * Get document chunks for a specific document (read-only)
 */
router.get('/document-chunks/:documentId', async (req, res) => {
  try {
    const { documentId } = req.params;

    const chunks = await VectorDatabaseModel.getDocumentChunks(documentId);

    return res.json({ chunks });
  } catch (error) {
    logger.error('Failed to get document chunks', error);
    return res.status(500).json({ error: 'Failed to get document chunks' });
  }
});

/**
 * GET /api/vector/analytics
 * Get search analytics for the current user (read-only)
 */
router.get('/analytics', async (req, res) => {
  try {
    const userId = req.user?.uid;
    const { days = 30 } = req.query;

    if (!userId) {
      return res.status(401).json({ error: 'User not authenticated' });
    }

    const analytics = await VectorDatabaseModel.getSearchAnalytics(
      userId,
      parseInt(days as string)
    );

    return res.json({ analytics });
  } catch (error) {
    logger.error('Failed to get analytics', error);
    return res.status(500).json({ error: 'Failed to get analytics' });
  }
});

/**
 * GET /api/vector/stats
 * Get vector database statistics (read-only)
 */
router.get('/stats', async (_req, res) => {
  try {
    const stats = {
      totalChunks: await VectorDatabaseModel.getTotalChunkCount(),
      totalDocuments: await VectorDatabaseModel.getTotalDocumentCount(),
      averageChunkSize: await VectorDatabaseModel.getAverageChunkSize()
    };

    return res.json({ stats });
  } catch (error) {
    logger.error('Failed to get vector database stats', error);
    return res.status(500).json({ error: 'Failed to get vector database stats' });
  }
});

export default router; 