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

/**
 * Fetch patients 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.PatientID] - Filter by patient ID
 * @param {string} [params.Name] - Search by patient name
 * @param {string} [params.Birthdate] - Filter by birthdate (YYYY-MM-DD)
 * @returns {Promise<Object>} API response with patient data and pagination
 */
export async function fetchPatients(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/patient?${query}` : '/api/patient');
}

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

/**
 * Check if a patient exists by PatientID or Email
 * @param {Object} params - Check parameters
 * @param {string} [params.PatientID] - Patient ID to check
 * @param {string} [params.EmailAddress1] - Email to check
 * @returns {Promise<Object>} API response with exists flag
 */
export async function checkPatientExists(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/patient/check?${query}` : '/api/patient/check');
}

/**
 * Create a new patient
 * @param {Object} data - Patient data
 * @param {string} data.PatientID - Patient identifier (required)
 * @param {string} data.NameFirst - First name (required)
 * @param {string} data.Sex - Sex code: '1' (Female) or '2' (Male) (required)
 * @param {string} data.Birthdate - Birthdate in ISO 8601 format (required)
 * @param {string} [data.AlternatePID] - Alternate patient ID
 * @param {string} [data.Prefix] - Title prefix (Mr, Mrs, Ms, Dr, Prof)
 * @param {string} [data.NameMiddle] - Middle name
 * @param {string} [data.NameLast] - Last name
 * @param {string} [data.NameMaiden] - Maiden name
 * @param {string} [data.Suffix] - Name suffix
 * @param {string} [data.PlaceOfBirth] - Place of birth
 * @param {string} [data.Citizenship] - Citizenship
 * @param {string} [data.Street_1] - Street address line 1
 * @param {string} [data.Street_2] - Street address line 2
 * @param {string} [data.Street_3] - Street address line 3
 * @param {string} [data.ZIP] - ZIP code
 * @param {string} [data.Province] - Province area code
 * @param {string} [data.City] - City area code
 * @param {string} [data.Country] - Country
 * @param {string} [data.Phone] - Phone number
 * @param {string} [data.MobilePhone] - Mobile phone number
 * @param {string} [data.EmailAddress1] - Primary email
 * @param {string} [data.EmailAddress2] - Secondary email
 * @param {Object} [data.PatIdt] - Patient identifier object
 * @param {string} [data.PatIdt.IdentifierType] - Identifier type (KTP, PASS, SSN, SIM, KTAS)
 * @param {string} [data.PatIdt.Identifier] - Identifier value
 * @param {string} [data.Race] - Race
 * @param {string} [data.MaritalStatus] - Marital status (A, B, D, M, S, W)
 * @param {string} [data.Religion] - Religion
 * @param {string} [data.Ethnic] - Ethnicity
 * @param {string} [data.DeathIndicator] - Death indicator (Y/N)
 * @param {string} [data.TimeOfDeath] - Time of death
 * @param {string} [data.PatCom] - Patient comments
 * @returns {Promise<Object>} API response
 */
export async function createPatient(data) {
  return post('/api/patient', data);
}

/**
 * Update an existing patient
 * @param {Object} data - Patient data (must include PatientID)
 * @returns {Promise<Object>} API response
 */
export async function updatePatient(id, data) {
  return patch(`/api/patient/${id}`, data);
}

/**
 * Delete a patient (soft delete)
 * @param {number} internalPid - Internal patient ID
 * @returns {Promise<Object>} API response
 */
export async function deletePatient(internalPid) {
  return del('/api/patient', { body: JSON.stringify({ InternalPID: internalPid }) });
}
