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

/**
 * Fetch specimens 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.SpecimenID] - Filter by specimen ID
 * @param {string} [params.SpecimenType] - Filter by specimen type
 * @param {string} [params.Status] - Filter by status
 * @param {string} [params.OrderID] - Filter by order ID
 * @returns {Promise<Object>} API response with specimen data and pagination
 */
export async function fetchSpecimens(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/specimen?${query}` : '/api/specimen');
}

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

/**
 * Create a new specimen
 * @param {Object} data - Specimen data
 * @param {string} data.SpecimenID - Specimen identifier (required)
 * @param {string} data.SpecimenType - Type of specimen (required)
 * @param {string} [data.OrderID] - Associated order ID
 * @param {string} [data.Status] - Specimen status
 * @param {string} [data.CollectionDate] - Collection date/time
 * @param {string} [data.CollectedBy] - Collector identifier
 * @param {string} [data.ContainerType] - Container type
 * @param {string} [data.Volume] - Volume collected
 * @param {string} [data.Units] - Volume units
 * @param {string} [data.Comment] - Comments
 * @returns {Promise<Object>} API response
 */
export async function createSpecimen(data) {
  return post('/api/specimen', data);
}

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

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

/**
 * Fetch specimen types for dropdown
 * @param {Object} params - Query parameters
 * @returns {Promise<Object>} API response with specimen types
 */
export async function fetchSpecimenTypes(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/specimen/type?${query}` : '/api/specimen/type');
}

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

/**
 * Fetch specimen collections for dropdown
 * @param {Object} params - Query parameters
 * @returns {Promise<Object>} API response with collection methods
 */
export async function fetchSpecimenCollections(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/specimen/collection?${query}` : '/api/specimen/collection');
}
