/**
 * Document Search Routes
 * Full-text search and advanced filtering
 */

import { FastifyInstance } from 'fastify';
import { searchDocuments, getSearchSuggestions } from '@the-order/database';
import { authenticateJWT } from '@the-order/shared';

export async function registerSearchRoutes(server: FastifyInstance): Promise<void> {
  server.post('/search', {
    preHandler: [authenticateJWT],
    schema: {
      body: {
        type: 'object',
        required: ['query'],
        properties: {
          query: { type: 'string' },
          filters: { type: 'object' },
          limit: { type: 'number' },
          offset: { type: 'number' },
        },
      },
      tags: ['search'],
    },
  }, async (request, reply) => {
    const body = request.body as any;
    const results = await searchDocuments(body);
    return reply.send(results);
  });

  server.get('/search/suggestions', {
    preHandler: [authenticateJWT],
    schema: {
      querystring: { type: 'object', properties: { q: { type: 'string' } } },
      tags: ['search'],
    },
  }, async (request, reply) => {
    const { q } = request.query as { q?: string };
    const suggestions = await getSearchSuggestions(q || '');
    return reply.send({ suggestions });
  });
}

