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

/**
 * Fetch orders 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 {number} [params.InternalPID] - Filter by internal patient ID
 * @param {string} [params.OrderStatus] - Filter by order status (ORD, SCH, ANA, VER, REV, REP)
 * @param {string} [params.include] - Include details (set to 'details' to include specimens and tests)
 * @returns {Promise<Object>} API response with orders data and pagination
 */
export async function fetchOrders(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/ordertest?${query}` : '/api/ordertest');
}

/**
 * Fetch a single order by ID with details
 * @param {string} id - Order ID (e.g., 0025030300001)
 * @returns {Promise<Object>} API response with order details including specimens and tests
 */
export async function fetchOrderById(id) {
  return get(`/api/ordertest/${id}`);
}

/**
 * Create a new order with specimens and tests
 * @param {Object} data - Order data
 * @param {number} data.InternalPID - Patient internal ID (required)
 * @param {Array<Object>} data.Tests - Array of test objects with TestSiteID (required)
 * @param {string} [data.OrderID] - Optional custom order ID (auto-generated if not provided)
 * @param {number} [data.PatVisitID] - Visit ID
 * @param {number} [data.SiteID=1] - Site ID
 * @param {string} [data.PlacerID] - Placer ID
 * @param {string} [data.Priority='R'] - Priority (R: Routine, S: Stat, U: Urgent)
 * @param {string} [data.ReqApp] - Requesting application
 * @param {string} [data.Comment] - Order comment
 * @returns {Promise<Object>} API response with created order data
 */
export async function createOrder(data) {
  return post('/api/ordertest', data);
}

/**
 * Update an existing order
 * @param {Object} data - Order data
 * @param {string} data.OrderID - Order ID (required)
 * @param {string} [data.Priority] - Priority (R, S, U)
 * @param {string} [data.OrderStatus] - Order status (ORD, SCH, ANA, VER, REV, REP)
 * @param {string} [data.OrderingProvider] - Ordering provider
 * @param {number} [data.DepartmentID] - Department ID
 * @param {number} [data.WorkstationID] - Workstation ID
 * @returns {Promise<Object>} API response with updated order data
 */
export async function updateOrder(id, data) {
  return patch(`/api/ordertest/${id}`, data);
}

/**
 * Delete an order
 * @param {string} orderId - Order ID to delete
 * @returns {Promise<Object>} API response
 */
export async function deleteOrder(orderId) {
  return del('/api/ordertest', { body: JSON.stringify({ OrderID: orderId }) });
}

/**
 * Update order status
 * @param {Object} data - Status update data
 * @param {string} data.OrderID - Order ID (required)
 * @param {string} data.OrderStatus - New status (ORD, SCH, ANA, VER, REV, REP)
 * @returns {Promise<Object>} API response with updated order data
 */
export async function updateOrderStatus(data) {
  return post('/api/ordertest/status', data);
}

/**
 * Order status mapping for display
 */
export const ORDER_STATUS = {
  ORD: { code: 'ORD', label: 'Ordered', color: 'badge-neutral', description: 'Order has been placed' },
  SCH: { code: 'SCH', label: 'Scheduled', color: 'badge-info', description: 'Order is scheduled' },
  ANA: { code: 'ANA', label: 'Analysis', color: 'badge-warning', description: 'Analysis in progress' },
  VER: { code: 'VER', label: 'Verified', color: 'badge-success', description: 'Results verified' },
  REV: { code: 'REV', label: 'Reviewed', color: 'badge-primary', description: 'Results reviewed' },
  REP: { code: 'REP', label: 'Reported', color: 'badge-secondary', description: 'Report generated' }
};

/**
 * Order priority mapping for display
 */
export const ORDER_PRIORITY = {
  R: { code: 'R', label: 'Routine', color: 'badge-neutral' },
  S: { code: 'S', label: 'Stat', color: 'badge-error' },
  U: { code: 'U', label: 'Urgent', color: 'badge-warning' }
};

/**
 * Get status display info
 * @param {string} statusCode - Status code
 * @returns {Object} Status display info
 */
export function getStatusInfo(statusCode) {
  return ORDER_STATUS[statusCode] || { code: statusCode, label: statusCode, color: 'badge-ghost' };
}

/**
 * Get priority display info
 * @param {string} priorityCode - Priority code
 * @returns {Object} Priority display info
 */
export function getPriorityInfo(priorityCode) {
  return ORDER_PRIORITY[priorityCode] || { code: priorityCode, label: priorityCode, color: 'badge-ghost' };
}
