import { apiClient } from '@/services/api/client';

// FE-PAGE-013: Add Search page - Search Service
// FE-API-008: Search service integration

export interface SearchUsersParams {
  query?: string;
  page?: number;
  limit?: number;
}

export interface SearchUsersResponse {
  users: Array<{
    id: string;
    username: string;
    email: string;
    first_name?: string;
    last_name?: string;
    avatar_url?: string;
    bio?: string;
    created_at: string;
  }>;
  total: number;
  page?: number;
  limit?: number;
}

/**
 * Search users by username, email, first_name, or last_name
 */
export async function searchUsers(
  params: SearchUsersParams,
): Promise<SearchUsersResponse> {
  const queryParams = new URLSearchParams();
  if (params.query) queryParams.append('q', params.query);
  if (params.page) queryParams.append('page', params.page.toString());
  if (params.limit) queryParams.append('limit', params.limit.toString());

  const response = await apiClient.get<{
    users: SearchUsersResponse['users'];
    total: number;
  }>(`/users/search?${queryParams.toString()}`);
  return {
    users: response.data.users || [],
    total: response.data.total || 0,
    page: params.page || 1,
    limit: params.limit || 20,
  };
}
