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

/**
 * Fetch list of equipment with optional filters
 * @param {Object} params - Filter parameters
 * @param {string} [params.IEID] - Filter by IEID
 * @param {string} [params.InstrumentName] - Filter by instrument name
 * @param {number} [params.DepartmentID] - Filter by department ID
 * @param {number} [params.WorkstationID] - Filter by workstation ID
 * @param {number} [params.Enable] - Filter by enable status (0 or 1)
 * @returns {Promise<Object>} List of equipment
 */
export async function fetchEquipmentList(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/equipmentlist?${query}` : '/api/equipmentlist');
}

/**
 * Fetch single equipment by ID
 * @param {number} id - Equipment ID (EID)
 * @returns {Promise<Object>} Equipment details
 */
export async function fetchEquipment(id) {
  return get(`/api/equipmentlist/${id}`);
}

/**
 * Create new equipment
 * @param {Object} data - Equipment data
 * @param {string} data.IEID - Internal Equipment ID (required)
 * @param {number} data.DepartmentID - Department ID (required)
 * @param {number} data.Enable - Enable status 0 or 1 (required)
 * @param {string} data.EquipmentRole - Equipment role code (required)
 * @param {string} [data.InstrumentID] - Instrument identifier
 * @param {string} [data.InstrumentName] - Instrument display name
 * @param {number} [data.WorkstationID] - Workstation ID
 * @returns {Promise<Object>} Created equipment ID
 */
export async function createEquipment(data) {
  const payload = {
    IEID: data.IEID,
    DepartmentID: data.DepartmentID,
    Enable: data.Enable,
    EquipmentRole: data.EquipmentRole,
    InstrumentID: data.InstrumentID || null,
    InstrumentName: data.InstrumentName || null,
    WorkstationID: data.WorkstationID || null,
  };
  return post('/api/equipmentlist', payload);
}

/**
 * Update existing equipment
 * @param {Object} data - Equipment data
 * @param {number} data.EID - Equipment ID (required)
 * @param {string} [data.IEID] - Internal Equipment ID
 * @param {number} [data.DepartmentID] - Department ID
 * @param {number} [data.Enable] - Enable status 0 or 1
 * @param {string} [data.EquipmentRole] - Equipment role code
 * @param {string} [data.InstrumentID] - Instrument identifier
 * @param {string} [data.InstrumentName] - Instrument display name
 * @param {number} [data.WorkstationID] - Workstation ID
 * @returns {Promise<Object>} Updated equipment ID
 */
export async function updateEquipment(id, data) {
  const payload = {
    IEID: data.IEID,
    DepartmentID: data.DepartmentID,
    Enable: data.Enable,
    EquipmentRole: data.EquipmentRole,
    InstrumentID: data.InstrumentID || null,
    InstrumentName: data.InstrumentName || null,
    WorkstationID: data.WorkstationID || null,
  };
  return patch(`/api/equipmentlist/${id}`, payload);
}

/**
 * Delete equipment (soft delete)
 * @param {number} eid - Equipment ID
 * @returns {Promise<Object>} Delete result
 */
export async function deleteEquipment(eid) {
  return del('/api/equipmentlist', { EID: eid });
}
