// src/tests/setup/globalApiMock.ts
import { vi } from 'vitest';

/**
 * Mocks the entire apiClient module.
 * This global mock is loaded for all tests via the `setupFiles` config in vitest.config.ts.
 * It prevents test failures in components that use providers (like FlyersProvider, AuthProvider)
 * which make API calls on mount when using `renderWithProviders`.
 *
 * Individual tests can override specific functions as needed, for example:
 *
 * import { vi } from 'vitest';
 * import * as apiClient from '../services/apiClient';
 *
 * const mockedApiClient = vi.mocked(apiClient);
 *
 * it('should test something', () => {
 *   mockedApiClient.someFunction.mockResolvedValue({ ... });
 *   // ... rest of the test
 * });
 */
/**
 * Helper to create a mock API response in the standard format.
 * API responses are wrapped in { success: true, data: ... } per ADR-028.
 */
const mockApiResponse = <T>(data: T): Response =>
  new Response(JSON.stringify({ success: true, data }));

// Global mock for apiClient - provides defaults for tests using renderWithProviders.
// Note: Individual test files must also call vi.mock() with their relative path.
vi.mock('../../services/apiClient', () => ({
  // --- Provider Mocks (with default successful responses) ---
  // These are essential for any test using renderWithProviders, as AppProviders
  // will mount all these data providers.
  // All responses use the standard API format: { success: true, data: ... }
  fetchFlyers: vi.fn(() => Promise.resolve(mockApiResponse([]))),
  fetchMasterItems: vi.fn(() => Promise.resolve(mockApiResponse([]))),
  fetchWatchedItems: vi.fn(() => Promise.resolve(mockApiResponse([]))),
  fetchShoppingLists: vi.fn(() => Promise.resolve(mockApiResponse([]))),
  getAuthenticatedUserProfile: vi.fn(() => Promise.resolve(mockApiResponse(null))),
  fetchCategories: vi.fn(() => Promise.resolve(mockApiResponse([]))), // For CorrectionsPage
  fetchAllBrands: vi.fn(() => Promise.resolve(mockApiResponse([]))), // For AdminBrandManager

  // --- General Mocks (return empty vi.fn() by default) ---
  // These functions are commonly used and can be implemented in specific tests.
  suggestRecipe: vi.fn(),
  getApplicationStats: vi.fn(),
  getSuggestedCorrections: vi.fn(),
  approveCorrection: vi.fn(),
  rejectCorrection: vi.fn(),
  updateSuggestedCorrection: vi.fn(),
  pingBackend: vi.fn(),
  checkStorage: vi.fn(),
  checkDbPoolHealth: vi.fn(),
  checkPm2Status: vi.fn(),
  checkRedisHealth: vi.fn(),
  checkDbSchema: vi.fn(),
  loginUser: vi.fn(),
  registerUser: vi.fn(),
  requestPasswordReset: vi.fn(),
  triggerFailingJob: vi.fn(),
  clearGeocodeCache: vi.fn(),
  uploadBrandLogo: vi.fn(),
  fetchActivityLog: vi.fn(),
  updateUserProfile: vi.fn(),
  updateUserPassword: vi.fn(),
  updateUserPreferences: vi.fn(),
  exportUserData: vi.fn(),
  deleteUserAccount: vi.fn(),
  getUserAddress: vi.fn(),
  updateUserAddress: vi.fn(),
  geocodeAddress: vi.fn(),
  getFlyersForReview: vi.fn(),
  fetchLeaderboard: vi.fn(),
  // --- Added to fix "No export is defined on the mock" errors ---
  fetchFlyerItems: vi.fn(),
  createShoppingList: vi.fn(),
  deleteShoppingList: vi.fn(),
  addShoppingListItem: vi.fn(),
  updateShoppingListItem: vi.fn(),
  removeShoppingListItem: vi.fn(),
  addWatchedItem: vi.fn(),
  removeWatchedItem: vi.fn(),
  fetchBestSalePrices: vi.fn(),
  resetPassword: vi.fn(),
  getUserAchievements: vi.fn(),
  uploadAvatar: vi.fn(),
  countFlyerItemsForFlyers: vi.fn(),
  fetchFlyerItemsForFlyers: vi.fn(),
}));
