async function apiFetch(url: string, options?: RequestInit) {
  const res = await fetch(url, { credentials: 'include', ...options });
  const json = await res.json().catch(() => ({}));
  if (!res.ok) throw new Error(json.error || `Request failed: ${res.status}`);
  return json;
}

export async function listMenuItems(params: Record<string, string> = {}) {
  const qs = new URLSearchParams(params).toString();
  return apiFetch(`/api/menu/items${qs ? '?' + qs : ''}`);
}

export async function getMenuItem(id: string) {
  return apiFetch(`/api/menu/items/${id}`);
}

export async function listMenuCategories() {
  return apiFetch('/api/menu/categories');
}

export async function createMenuItem(body: Record<string, unknown> | FormData) {
  const options: RequestInit = { method: 'POST', body: body instanceof FormData ? body : JSON.stringify(body) };
  if (!(body instanceof FormData)) {
    options.headers = { 'Content-Type': 'application/json' };
  }
  return apiFetch('/api/menu/items', options);
}

export async function updateMenuItem(id: string, body: Record<string, unknown> | FormData) {
  const options: RequestInit = { method: 'PATCH', body: body instanceof FormData ? body : JSON.stringify(body) };
  if (!(body instanceof FormData)) {
    options.headers = { 'Content-Type': 'application/json' };
  }
  return apiFetch(`/api/menu/items/${id}`, options);
}

export async function deleteMenuItem(id: string) {
  return apiFetch(`/api/menu/items/${id}`, { method: 'DELETE' });
}

export async function createMenuCategory(body: Record<string, unknown>) {
  return apiFetch('/api/menu/categories', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
}

export async function updateMenuCategory(id: string, body: Record<string, unknown>) {
  return apiFetch(`/api/menu/categories/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
}

export async function deleteMenuCategory(id: string) {
  return apiFetch(`/api/menu/categories/${id}`, { method: 'DELETE' });
}

export async function listModifierGroups(params: Record<string, string> = {}) {
  const qs = new URLSearchParams(params).toString();
  return apiFetch(`/api/menu/modifier-groups${qs ? '?' + qs : ''}`);
}

export async function createModifierGroup(body: Record<string, unknown>) {
  return apiFetch('/api/menu/modifier-groups', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
}

export async function updateModifierGroup(id: string, body: Record<string, unknown>) {
  return apiFetch(`/api/menu/modifier-groups/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
}

export async function deleteModifierGroup(id: string) {
  return apiFetch(`/api/menu/modifier-groups/${id}`, { method: 'DELETE' });
}

export async function createModifierOption(groupId: string, body: Record<string, unknown>) {
  return apiFetch(`/api/menu/modifier-groups/${groupId}/options`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
}

export async function updateModifierOption(groupId: string, optId: string, body: Record<string, unknown>) {
  return apiFetch(`/api/menu/modifier-groups/${groupId}/options/${optId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
}

export async function deleteModifierOption(groupId: string, optId: string) {
  return apiFetch(`/api/menu/modifier-groups/${groupId}/options/${optId}`, { method: 'DELETE' });
}
