import { apiClient } from './api-client';
import type { z } from 'zod/v4';
import type {
  PantryItemResponseSchema,
  PantryItemListResponseSchema,
  WasteStatsResponseSchema,
  BatchTransitionResponseSchema,
  CreatePantryItemInput,
  UpdatePantryItemInput,
  TransitionPantryItemInput,
  BatchTransitionInput,
} from '@meshitrack/shared';

type PantryItemResponse = z.infer<typeof PantryItemResponseSchema>;
type PantryItemListResponse = z.infer<typeof PantryItemListResponseSchema>;
type WasteStatsResponse = z.infer<typeof WasteStatsResponseSchema>;
type BatchTransitionResponse = z.infer<typeof BatchTransitionResponseSchema>;

export interface PantryQuery {
  storageLocation?: string;
  status?: string;
  urgency?: string;
  productId?: string;
  cursor?: string;
  limit?: number;
}

export async function listPantryItems(
  householdId: string,
  query?: PantryQuery,
): Promise<PantryItemListResponse> {
  const params = new URLSearchParams();
  if (query?.storageLocation) params.set('storageLocation', query.storageLocation);
  if (query?.status) params.set('status', query.status);
  if (query?.urgency) params.set('urgency', query.urgency);
  if (query?.productId) params.set('productId', query.productId);
  if (query?.cursor) params.set('cursor', query.cursor);
  if (query?.limit) params.set('limit', String(query.limit));
  const qs = params.toString();
  return apiClient.get<PantryItemListResponse>(
    `/households/${householdId}/pantry${qs ? `?${qs}` : ''}`,
  );
}

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

export async function createPantryItem(
  householdId: string,
  data: CreatePantryItemInput,
): Promise<PantryItemResponse> {
  return apiClient.post<PantryItemResponse>(`/households/${householdId}/pantry`, data);
}

export async function updatePantryItem(
  householdId: string,
  id: string,
  data: UpdatePantryItemInput,
): Promise<PantryItemResponse> {
  return apiClient.patch<PantryItemResponse>(`/households/${householdId}/pantry/${id}`, data);
}

export async function transitionPantryItem(
  householdId: string,
  id: string,
  data: TransitionPantryItemInput,
): Promise<PantryItemResponse> {
  return apiClient.post<PantryItemResponse>(
    `/households/${householdId}/pantry/${id}/transition`,
    data,
  );
}

export async function batchTransitionPantryItems(
  householdId: string,
  data: BatchTransitionInput,
): Promise<BatchTransitionResponse> {
  return apiClient.post<BatchTransitionResponse>(
    `/households/${householdId}/pantry/batch-transition`,
    data,
  );
}

export async function getExpiringSoon(
  householdId: string,
  query?: { days?: number; cursor?: string; limit?: number },
): Promise<PantryItemListResponse> {
  const params = new URLSearchParams();
  if (query?.days !== undefined) params.set('days', String(query.days));
  if (query?.cursor) params.set('cursor', query.cursor);
  if (query?.limit) params.set('limit', String(query.limit));
  const qs = params.toString();
  return apiClient.get<PantryItemListResponse>(
    `/households/${householdId}/pantry/expiring-soon${qs ? `?${qs}` : ''}`,
  );
}

export async function getWasteStats(
  householdId: string,
  period?: string,
): Promise<WasteStatsResponse> {
  const params = new URLSearchParams();
  if (period) params.set('period', period);
  const qs = params.toString();
  return apiClient.get<WasteStatsResponse>(
    `/households/${householdId}/pantry/stats${qs ? `?${qs}` : ''}`,
  );
}

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