import { apiFetch } from '@client/lib/apiError';

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' });
}

// Task #291: branch availability overrides for menu items.
export async function listMenuItemBranches(itemId: string) {
  return apiFetch(`/api/menu/items/${itemId}/branches`);
}

export async function setMenuItemBranchAvailability(itemId: string, branchId: string, isAvailable: boolean) {
  return apiFetch(`/api/menu/items/${itemId}/branches/${branchId}`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ is_available: isAvailable }),
  });
}

export async function clearMenuItemBranchAvailability(itemId: string, branchId: string) {
  return apiFetch(`/api/menu/items/${itemId}/branches/${branchId}`, { method: 'DELETE' });
}

// Task #305: low-stock dashboard widget + one-click restock.
export interface LowStockItem {
  id: string;
  name: string;
  category_name: string | null;
  stock_count: number | null;
  low_stock_threshold: number | null;
  daily_reset_count: number | null;
  auto_disabled_at_zero: boolean;
  is_in_stock: boolean;
  last_low_stock_alert_at: string | null;
}

export async function listLowStockItems(): Promise<{ items: LowStockItem[] }> {
  return apiFetch('/api/menu/low-stock');
}

export async function restockMenuItem(id: string, target?: number) {
  return apiFetch(`/api/menu/items/${id}/restock`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(target == null ? {} : { target }),
  });
}
