import { apiClient } from './client';
import {
  CreatePatientInput,
  CreatePatientResponse,
  Patient,
  PatientAppointmentHistoryResponse,
  PatientsListResponse,
} from '@/types/patient';

export const patientsApi = {
  list: async (params?: { q?: string; page?: number; limit?: number }): Promise<PatientsListResponse> => {
    const response = await apiClient.get('/patients', { params });
    return response.data;
  },

  create: async (data: CreatePatientInput): Promise<CreatePatientResponse> => {
    const { email, ...rest } = data;
    const body = {
      ...rest,
      ...(email?.trim() ? { email: email.trim() } : {}),
    };
    const response = await apiClient.post('/patients', body);
    return response.data;
  },

  getOne: async (id: string): Promise<{ success: boolean; data: Patient }> => {
    const response = await apiClient.get(`/patients/${id}`);
    return response.data;
  },

  listAppointments: async (patientId: string): Promise<PatientAppointmentHistoryResponse> => {
    const response = await apiClient.get(`/patients/${patientId}/appointments`);
    return response.data;
  },
};
