import { apiClient } from './api-client';
import type { z } from 'zod/v4';
import type {
  RefillAlertResponseSchema,
  RefillListResponseSchema,
  RefillListListResponseSchema,
  AddToCabinetResponseSchema,
  CreateRefillListSchema,
  UpdateRefillListSchema,
  UpdateRefillListItemSchema,
} from '@meshitrack/shared';

type RefillAlertResponse = z.infer<typeof RefillAlertResponseSchema>;
type RefillListResponse = z.infer<typeof RefillListResponseSchema>;
type RefillListListResponse = z.infer<typeof RefillListListResponseSchema>;
type AddToCabinetResponse = z.infer<typeof AddToCabinetResponseSchema>;
type CreateRefillListInput = z.infer<typeof CreateRefillListSchema>;
type UpdateRefillListInput = z.infer<typeof UpdateRefillListSchema>;
type UpdateRefillListItemInput = z.infer<typeof UpdateRefillListItemSchema>;

export async function getRefillAlerts(
  householdId: string,
  query?: { thresholdDays?: number; userId?: string },
): Promise<{ data: RefillAlertResponse[] }> {
  const params = new URLSearchParams();
  if (query?.thresholdDays) params.set('thresholdDays', String(query.thresholdDays));
  if (query?.userId) params.set('userId', query.userId);
  const qs = params.toString();
  return apiClient.get<{ data: RefillAlertResponse[] }>(
    `/households/${householdId}/refills/alerts${qs ? `?${qs}` : ''}`,
  );
}

export async function listRefillLists(
  householdId: string,
  query?: { status?: string; cursor?: string; limit?: number },
): Promise<RefillListListResponse> {
  const params = new URLSearchParams();
  if (query?.status) params.set('status', query.status);
  if (query?.cursor) params.set('cursor', query.cursor);
  if (query?.limit) params.set('limit', String(query.limit));
  const qs = params.toString();
  return apiClient.get<RefillListListResponse>(
    `/households/${householdId}/refills/lists${qs ? `?${qs}` : ''}`,
  );
}

export async function createRefillList(
  householdId: string,
  data: CreateRefillListInput,
): Promise<RefillListResponse> {
  return apiClient.post<RefillListResponse>(`/households/${householdId}/refills/lists`, data);
}

export async function getRefillList(householdId: string, id: string): Promise<RefillListResponse> {
  return apiClient.get<RefillListResponse>(`/households/${householdId}/refills/lists/${id}`);
}

export async function updateRefillList(
  householdId: string,
  id: string,
  data: UpdateRefillListInput,
): Promise<RefillListResponse> {
  return apiClient.patch<RefillListResponse>(
    `/households/${householdId}/refills/lists/${id}`,
    data,
  );
}

export async function updateRefillListItem(
  householdId: string,
  listId: string,
  itemId: string,
  data: UpdateRefillListItemInput,
): Promise<RefillListResponse> {
  return apiClient.patch<RefillListResponse>(
    `/households/${householdId}/refills/lists/${listId}/items/${itemId}`,
    data,
  );
}

export async function addToCabinet(
  householdId: string,
  listId: string,
): Promise<AddToCabinetResponse> {
  return apiClient.post<AddToCabinetResponse>(
    `/households/${householdId}/refills/lists/${listId}/add-to-cabinet`,
    {},
  );
}
