import client from '../client';
import type { PaginatedResponse } from '../types';

// --- Types ---

export interface MedicationReminder {
  id: string;
  patient_id: string;
  medication_name: string;
  dosage?: string;
  frequency: string;
  reminder_times: unknown;
  start_date?: string;
  end_date?: string;
  is_active: boolean;
  notes?: string;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateMedicationReminderReq {
  patient_id: string;
  medication_name: string;
  dosage?: string;
  frequency?: string;
  reminder_times?: unknown;
  start_date?: string;
  end_date?: string;
  is_active?: boolean;
  notes?: string;
}

export interface UpdateMedicationReminderReq {
  medication_name?: string;
  dosage?: string;
  frequency?: string;
  reminder_times?: unknown;
  start_date?: string;
  end_date?: string;
  is_active?: boolean;
  notes?: string;
}

// --- API ---

export const medicationReminderApi = {
  list: async (patientId: string, params?: { page?: number; page_size?: number }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<MedicationReminder>;
    }>(`/health/patients/${patientId}/medication-reminders`, { params });
    return data.data;
  },

  create: async (req: CreateMedicationReminderReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: MedicationReminder;
    }>('/health/medication-reminders', req);
    return data.data;
  },

  update: async (id: string, req: UpdateMedicationReminderReq & { version: number }) => {
    const { data } = await client.put<{
      success: boolean;
      data: MedicationReminder;
    }>(`/health/medication-reminders/${id}`, req);
    return data.data;
  },

  delete: async (id: string, version: number) => {
    await client.delete(`/health/medication-reminders/${id}`, { data: { version } });
  },
};
