/*
 * File: patientAPI.ts
 * Description: API service for patient care operations using apisauce
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

import { create } from 'apisauce';
import { API_CONFIG, buildHeaders } from '../../../shared/utils';

const api = create({
  baseURL: API_CONFIG.BASE_URL
});

/**
 * Patient API Service
 * 
 * Purpose: Handle all patient-related API operations
 * 
 * Features:
 * - Get patient list with filtering
 * - Get individual patient details
 * - Update patient information
 * - Get patient vital signs
 * - Get patient medical history
 */
export const patientAPI = {
  /**
   * Get Patients
   * 
   * Purpose: Fetch list of medical cases from server
   * 
   * @param token - Authentication token
   * @returns Promise with medical cases data
   */
  getPatients: (token: string) => {
    return api.get('/api/ai-cases/all-patients', {}, buildHeaders({ token }));
  },

  /**
   * Get Patient Details by ID
   * 
   * Purpose: Fetch detailed information for a specific patient by ID
   * 
   * @param patientId - Patient ID
   * @param token - Authentication token
   * @returns Promise with patient details including predictions and series
   */
  getPatientDetailsById: (patientId: string, token: string) => {
    return api.get(`/api/ai-cases/patient/${patientId}/predictions`, {}, buildHeaders({ token }));
  },

  /**
   * Get Patient Details (Legacy - kept for backward compatibility)
   * 
   * Purpose: Fetch detailed information for a specific patient
   * 
   * @param patientId - Patient ID
   * @param token - Authentication token
   * @returns Promise with patient details
   */
  getPatientDetails: (patientId: string, token: string) => {
    return api.get(`/api/ai-cases/patient/${patientId}/predictions`, {}, buildHeaders({ token }));
  },

  /**
   * Update Patient
   * 
   * Purpose: Update patient information
   * 
   * @param patientId - Patient ID
   * @param patientData - Updated patient data
   * @param token - Authentication token
   * @returns Promise with updated patient data
   */
  updatePatient: (patientId: string, patientData: any, token: string) => {
    return api.put(`/api/patients/${patientId}`, patientData, buildHeaders({ token }));
  },

  /**
   * Get Patient Vital Signs
   * 
   * Purpose: Fetch latest vital signs for a patient
   * 
   * @param patientId - Patient ID
   * @param token - Authentication token
   * @returns Promise with vital signs data
   */
  getPatientVitals: (patientId: string, token: string) => {
    return api.get(`/api/patients/${patientId}/vitals`, {}, buildHeaders({ token }));
  },

  /**
   * Update Patient Vital Signs
   * 
   * Purpose: Add new vital signs reading for a patient
   * 
   * @param patientId - Patient ID
   * @param vitalSigns - Vital signs data
   * @param token - Authentication token
   * @returns Promise with updated vital signs
   */
  updatePatientVitals: (patientId: string, vitalSigns: any, token: string) => {
    return api.put(`/api/patients/${patientId}/vitals`, vitalSigns, buildHeaders({ token }));
  },

  /**
   * Submit Feedback for AI Prediction
   * 
   * Purpose: Submit physician feedback for AI predictions
   * 
   * @param feedbackData - Feedback payload
   * @returns API response
   */
  submitFeedback: (feedbackData: {
    patid: string;
    prediction_id: number;
    feedback_text: string;
    is_positive: boolean;
  },token) => api.post('/api/ai-cases/feedbacks', feedbackData, buildHeaders({ token })),

  /**
   * Get Patient Medical History
   * 
   * Purpose: Fetch medical history for a patient
   * 
   * @param patientId - Patient ID
   * @param token - Authentication token
   * @returns Promise with medical history data
   */
  getPatientHistory: (patientId: string, token: string) => {
    return api.get(`/api/patients/${patientId}/history`, {}, buildHeaders({ token }));
  },

  /**
   * Get Patient Medications
   * 
   * Purpose: Fetch current medications for a patient
   * 
   * @param patientId - Patient ID
   * @param token - Authentication token
   * @returns Promise with medications data
   */
  getPatientMedications: (patientId: string, token: string) => {
    return api.get(`/api/patients/${patientId}/medications`, {}, buildHeaders({ token }));
  },

  /**
   * Add Patient Medication
   * 
   * Purpose: Add new medication for a patient
   * 
   * @param patientId - Patient ID
   * @param medication - Medication data
   * @param token - Authentication token
   * @returns Promise with updated medications
   */
  addPatientMedication: (patientId: string, medication: any, token: string) => {
    return api.post(`/api/patients/${patientId}/medications`, medication, buildHeaders({ token }));
  },

  /**
   * Get Patient Allergies
   * 
   * Purpose: Fetch allergies for a patient
   * 
   * @param patientId - Patient ID
   * @param token - Authentication token
   * @returns Promise with allergies data
   */
  getPatientAllergies: (patientId: string, token: string) => {
    return api.get(`/api/patients/${patientId}/allergies`, {}, buildHeaders({ token }));
  },

  /**
   * Search Patients
   * 
   * Purpose: Search patients by various criteria
   * 
   * @param query - Search query
   * @param token - Authentication token
   * @returns Promise with search results
   */
  searchPatients: (query: string, token: string) => {
    return api.get('/api/patients/search', { q: query }, buildHeaders({ token }));
  },
};

// Legacy export for backward compatibility
export const caseAPI = patientAPI;

/*
 * End of File: patientAPI.ts
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */ 