import { apiClient } from './api-client';
import type { z } from 'zod/v4';
import type {
  RecipeResponseSchema,
  RecipeListResponseSchema,
  CreateRecipeInput,
  UpdateRecipeInput,
  ScaleRecipeInput,
  ImportRecipeTextInput,
  ImportRecipeUrlInput,
} from '@meshitrack/shared';

type RecipeResponse = z.infer<typeof RecipeResponseSchema>;
type RecipeListResponse = z.infer<typeof RecipeListResponseSchema>;

export interface RecipeQuery {
  q?: string;
  tags?: string;
  cuisine?: string;
  maxCalories?: number;
  isFavorite?: boolean;
  cursor?: string;
  limit?: number;
}

export async function listRecipes(
  householdId: string,
  query?: RecipeQuery,
): Promise<RecipeListResponse> {
  const params = new URLSearchParams();
  if (query?.q) params.set('q', query.q);
  if (query?.tags) params.set('tags', query.tags);
  if (query?.cuisine) params.set('cuisine', query.cuisine);
  if (query?.maxCalories !== undefined) params.set('maxCalories', String(query.maxCalories));
  if (query?.isFavorite !== undefined) params.set('isFavorite', String(query.isFavorite));
  if (query?.cursor) params.set('cursor', query.cursor);
  if (query?.limit) params.set('limit', String(query.limit));
  const qs = params.toString();
  return apiClient.get<RecipeListResponse>(
    `/households/${householdId}/recipes${qs ? `?${qs}` : ''}`,
  );
}

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

export async function createRecipe(
  householdId: string,
  data: CreateRecipeInput,
): Promise<RecipeResponse> {
  return apiClient.post<RecipeResponse>(`/households/${householdId}/recipes`, data);
}

export async function updateRecipe(
  householdId: string,
  id: string,
  data: UpdateRecipeInput,
): Promise<RecipeResponse> {
  return apiClient.patch<RecipeResponse>(`/households/${householdId}/recipes/${id}`, data);
}

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

export async function scaleRecipe(
  householdId: string,
  id: string,
  data: ScaleRecipeInput,
): Promise<RecipeResponse> {
  return apiClient.post<RecipeResponse>(`/households/${householdId}/recipes/${id}/scale`, data);
}

export async function importRecipeFromText(
  householdId: string,
  data: ImportRecipeTextInput,
): Promise<{ available: boolean; draft?: unknown }> {
  return apiClient.post(`/households/${householdId}/recipes/import-text`, data);
}

export async function importRecipeFromUrl(
  householdId: string,
  data: ImportRecipeUrlInput,
): Promise<{ available: boolean; draft?: unknown }> {
  return apiClient.post(`/households/${householdId}/recipes/import-url`, data);
}

export async function listRecipesByProduct(
  householdId: string,
  productId: string,
  query?: { cursor?: string; limit?: number },
): Promise<RecipeListResponse> {
  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<RecipeListResponse>(
    `/households/${householdId}/recipes/by-product/${productId}${qs ? `?${qs}` : ''}`,
  );
}
