import type { Recipe, RecipeDraft, Tag, ApiResponse, UrlImportResult, HarnessStatus } from '../types/recipe';

const API_BASE_URL = '/api';

export async function fetchRecipes(params?: {
  search?: string;
  offset?: number;
  limit?: number;
  tagId?: number | null;
}): Promise<Recipe[]> {
  const url = new URL(`${API_BASE_URL}/recipes`, window.location.origin);
  if (params?.search) {
    url.searchParams.set('search', params.search);
  }
  if (params?.offset !== undefined) {
    url.searchParams.set('offset', params.offset.toString());
  }
  if (params?.limit !== undefined) {
    url.searchParams.set('limit', params.limit.toString());
  }
  if (params?.tagId !== undefined && params?.tagId !== null) {
    url.searchParams.set('tagId', params.tagId.toString());
  }
  const response = await fetch(url.toString());
  if (!response.ok) {
    throw new Error(`Failed to fetch recipes: ${response.statusText}`);
  }
  const result: ApiResponse<Recipe[]> = await response.json();
  if (!result.success || !result.data) {
    throw new Error(result.error || 'Failed to fetch recipes');
  }
  return result.data;
}

export async function fetchRecipe(id: number): Promise<Recipe> { return {} as any; }
export async function createRecipe(recipe: RecipeDraft): Promise<Recipe> { return {} as any; }
export async function updateRecipe(id: number, updates: Partial<Omit<Recipe, 'id' | 'created_at' | 'updated_at'>>): Promise<Recipe> { return {} as any; }
export async function deleteRecipe(id: number): Promise<void> {}
export async function fetchTags(): Promise<Tag[]> { return []; }
export async function createTag(tag: Omit<Tag, 'id'>): Promise<Tag> { return { id: 0, name: '', color: tag.color }; }
export async function fetchRecipeTags(recipeId: number): Promise<Tag[]> { return []; }
export async function assignTagToRecipe(recipeId: number, tagId: number): Promise<void> {};
export async function removeTagFromRecipe(recipeId: number, tagId: number): Promise<void> {};
export async function importRecipeFromUrl(url: string): Promise<UrlImportResult> { return {title:'',ingredients:[],instructions:[]}; }
export async function fetchHarnessStatus(): Promise<HarnessStatus> { return {running:false,version:'',uptime:0}; }
