// [DEF:setupTests:Module]
// @TIER: STANDARD
// @PURPOSE: Global test setup with mocks for SvelteKit modules
// @LAYER: UI

import { vi } from 'vitest';

// Mock $app/environment
vi.mock('$app/environment', () => ({
  browser: true,
  dev: true,
  building: false
}));

// Mock $app/stores
vi.mock('$app/stores', () => {
  const { writable } = require('svelte/store');
  return {
    page: writable({ url: new URL('http://localhost'), params: {}, route: { id: 'test' } }),
    navigating: writable(null),
    updated: { check: vi.fn(), subscribe: writable(false).subscribe }
  };
});

// Mock $app/navigation
vi.mock('$app/navigation', () => ({
  goto: vi.fn(),
  push: vi.fn(),
  replace: vi.fn(),
  prefetch: vi.fn(),
  prefetchRoutes: vi.fn()
}));

// Mock localStorage
const localStorageMock = (() => {
  let store = {};
  return {
    getItem: vi.fn((key) => store[key] || null),
    setItem: vi.fn((key, value) => { store[key] = value; }),
    removeItem: vi.fn((key) => { delete store[key]; }),
    clear: () => { store = {}; },
    get length() { return Object.keys(store).length; },
    key: vi.fn((i) => Object.keys(store)[i] || null)
  };
})();

Object.defineProperty(global, 'localStorage', { value: localStorageMock });
Object.defineProperty(global, 'sessionStorage', { value: localStorageMock });

// Mock console.log to reduce noise in tests
const originalLog = console.log;
console.log = vi.fn((...args) => {
  // Keep activity store and task drawer logs for test output
  const firstArg = args[0];
  if (typeof firstArg === 'string' && 
      (firstArg.includes('[activityStore]') || 
       firstArg.includes('[taskDrawer]') ||
       firstArg.includes('[SidebarStore]'))) {
    originalLog.apply(console, args);
  }
});

// [/DEF:setupTests:Module]
