import { Patient } from "../types";
import api from "./api";

export const getPatientById = async (patientId: string): Promise<Patient> => {
    if (!patientId) {
      throw new Error("Patient ID is required");
    }
  
    try {
      const res = await api.get(`/patient/${patientId}`);
      
      const patient = res.data.patient ?? res.data;
  
      return patient;
    } catch (error: any) {
      console.error("Failed to fetch patient:", error);
      throw new Error(
        error?.response?.data?.message || "Failed to fetch patient"
      );
    }
  };

export const getAllPatients = async (): Promise<Patient[]> => {
  const response = await api.get("/patient");
  // Handle different response structures
  return response.data?.data || response.data?.patients || response.data || [];
};

export const getPatientsByDoctorId = async (
  doctorId: number
): Promise<Patient[]> => {
  const response = await api.get(`/patient/doctor/${doctorId}`);
  // Handle different response structures
  return response.data?.data || response.data?.patients || response.data || [];
};
