import api from './api';
import { Term, TermsResponse, TermFilters } from '../types/term';

/**
 * Fetch terms with optional filters and pagination
 */
export const fetchTerms = async (filters: TermFilters = {}): Promise<TermsResponse> => {
  const params = new URLSearchParams();
  
  if (filters.query) params.append('query', filters.query);
  if (filters.wordType) params.append('wordType', filters.wordType);
  if (filters.cefrLevel) params.append('cefrLevel', filters.cefrLevel);
  if (filters.page) params.append('page', filters.page.toString());
  if (filters.limit) params.append('limit', filters.limit.toString());

  const response = await api.get<TermsResponse>(`/api/terms?${params.toString()}`);
  return response.data;
};

/**
 * Fetch a single term by ID
 */
export const fetchTermById = async (id: string): Promise<Term> => {
  const response = await api.get<{ term: Term }>(`/api/terms/${id}`);
  return response.data.term;
};

