import { apiClient } from './api-client';
import type { z } from 'zod/v4';
import type {
  MedicineResponseSchema,
  MedicineListResponseSchema,
  MedicineProductResponseSchema,
  MedicineProductListResponseSchema,
  CreateMedicineInput,
  UpdateMedicineInput,
  CreateMedicineProductInput,
  UpdateMedicineProductInput,
} from '@meshitrack/shared';

type MedicineResponse = z.infer<typeof MedicineResponseSchema>;
type MedicineListResponse = z.infer<typeof MedicineListResponseSchema>;
type MedicineProductResponse = z.infer<typeof MedicineProductResponseSchema>;
type MedicineProductListResponse = z.infer<typeof MedicineProductListResponseSchema>;

export async function listMedicines(
  householdId: string,
  query?: { q?: string; category?: string; form?: string; cursor?: string; limit?: number },
): Promise<MedicineListResponse> {
  const params = new URLSearchParams();
  if (query?.q) params.set('q', query.q);
  if (query?.category) params.set('category', query.category);
  if (query?.form) params.set('form', query.form);
  if (query?.cursor) params.set('cursor', query.cursor);
  if (query?.limit) params.set('limit', String(query.limit));
  const qs = params.toString();
  return apiClient.get<MedicineListResponse>(
    `/households/${householdId}/medicines${qs ? `?${qs}` : ''}`,
  );
}

export async function getMedicine(householdId: string, id: string): Promise<MedicineResponse> {
  return apiClient.get<MedicineResponse>(`/households/${householdId}/medicines/${id}`);
}

export async function createMedicine(
  householdId: string,
  data: CreateMedicineInput,
): Promise<MedicineResponse> {
  return apiClient.post<MedicineResponse>(`/households/${householdId}/medicines`, data);
}

export async function updateMedicine(
  householdId: string,
  id: string,
  data: UpdateMedicineInput,
): Promise<MedicineResponse> {
  return apiClient.patch<MedicineResponse>(`/households/${householdId}/medicines/${id}`, data);
}

export async function deleteMedicine(householdId: string, id: string): Promise<void> {
  return apiClient.delete(`/households/${householdId}/medicines/${id}`);
}

export async function listMedicineProducts(
  householdId: string,
  medicineId: string,
  query?: { cursor?: string; limit?: number },
): Promise<MedicineProductListResponse> {
  const params = new URLSearchParams();
  if (query?.cursor) params.set('cursor', query.cursor);
  if (query?.limit) params.set('limit', String(query.limit));
  const qs = params.toString();
  return apiClient.get<MedicineProductListResponse>(
    `/households/${householdId}/medicines/${medicineId}/products${qs ? `?${qs}` : ''}`,
  );
}

export async function createMedicineProduct(
  householdId: string,
  medicineId: string,
  data: CreateMedicineProductInput,
): Promise<MedicineProductResponse> {
  return apiClient.post<MedicineProductResponse>(
    `/households/${householdId}/medicines/${medicineId}/products`,
    data,
  );
}

export async function updateMedicineProduct(
  householdId: string,
  id: string,
  data: UpdateMedicineProductInput,
): Promise<MedicineProductResponse> {
  return apiClient.patch<MedicineProductResponse>(
    `/households/${householdId}/medicine-products/${id}`,
    data,
  );
}

export async function deleteMedicineProduct(householdId: string, id: string): Promise<void> {
  return apiClient.delete(`/households/${householdId}/medicine-products/${id}`);
}
