import { apiClient } from '@/lib/api-client';
import type { UserFilters, UsersResponse, UserMutationPayload } from './types';

export async function getUsers(filters: UserFilters): Promise<UsersResponse> {
  const searchParams = new URLSearchParams();

  if (filters.page) searchParams.set('page', String(filters.page));
  if (filters.limit) searchParams.set('limit', String(filters.limit));
  if (filters.roles) searchParams.set('roles', filters.roles);
  if (filters.search) searchParams.set('search', filters.search);
  if (filters.sort) searchParams.set('sort', filters.sort);

  const query = searchParams.toString();

  return apiClient<UsersResponse>(`/users${query ? `?${query}` : ''}`);
}

export async function createUser(data: UserMutationPayload) {
  return apiClient<{ success: boolean; message: string }>('/users', {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateUser(id: string, data: UserMutationPayload) {
  return apiClient<{ success: boolean; message: string }>(`/users/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data)
  });
}

export async function deleteUser(id: string) {
  return apiClient<{ success: boolean; message: string }>(`/users/${id}`, {
    method: 'DELETE'
  });
}
