import { apiClient } from './client';

export interface SearchResult {
  type: 'contact' | 'mail' | 'file' | 'event';
  id: string;
  name: string;
  description?: string;
  url: string;
}

export interface SearchResponse {
  results: SearchResult[];
  facets?: Record<string, Array<{ value: string; count: number }>>;
  summary?: string;
  suggestions?: string[];
}

export async function search(query: string, entityTypes?: string[], limit = 20): Promise<SearchResponse> {
  const r = await apiClient.post('/search', {
    query,
    entity_types: entityTypes,
    limit,
  });
  return r.data;
}

export async function searchSuggest(q: string): Promise<string[]> {
  const r = await apiClient.get('/search/suggest', { params: { q } });
  return r.data.suggestions;
}

export async function searchSimilar(entityType: string, entityId: string): Promise<SearchResult[]> {
  const r = await apiClient.post('/search/similar', {
    entity_type: entityType,
    entity_id: entityId,
  });
  return r.data.similar;
}
