import { get, post, patch, del } from './client.js';

/**
 * Fetch users list with optional filters and pagination
 * @param {Object} params - Query parameters
 * @param {number} [params.page=1] - Page number
 * @param {number} [params.perPage=20] - Items per page
 * @param {string} [params.search] - Search by username, email, or name
 * @param {string} [params.role] - Filter by role
 * @param {string} [params.status] - Filter by status
 * @returns {Promise<Object>} API response with user data and pagination
 */
export async function fetchUsers(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/users?${query}` : '/api/users');
}

/**
 * Fetch a single user by ID
 * @param {number} id - User ID
 * @returns {Promise<Object>} API response with user details
 */
export async function fetchUser(id) {
  return get(`/api/users/${id}`);
}

/**
 * Create a new user
 * @param {Object} data - User data
 * @param {string} data.Username - Username (required)
 * @param {string} data.Email - Email address (required)
 * @param {string} data.Password - Password (required)
 * @param {string} [data.NameFirst] - First name
 * @param {string} [data.NameLast] - Last name
 * @param {string} [data.Role] - User role
 * @param {string} [data.Department] - Department
 * @param {string} [data.Status] - User status (active/inactive)
 * @returns {Promise<Object>} API response
 */
export async function createUser(data) {
  return post('/api/users', data);
}

/**
 * Update an existing user
 * @param {number} id - User ID
 * @param {Object} data - User data to update
 * @returns {Promise<Object>} API response
 */
export async function updateUser(id, data) {
  return patch(`/api/users/${id}`, data);
}

/**
 * Delete a user
 * @param {number} id - User ID
 * @returns {Promise<Object>} API response
 */
export async function deleteUser(id) {
  return del(`/api/users/${id}`);
}
