/**
 * Mock factory functions for Zustand stores
 * Used in component and hook tests
 */
import { vi } from 'vitest';

// ============================================================================
// WorkflowStore Mock
// ============================================================================

export interface MockWorkflowStoreState {
  // State
  id: number | null;
  name: string;
  isActive: boolean;
  initialName: string | null;
  isLoading: boolean;
  isSaving: boolean;
  isActivating: boolean;
  loadError: string | null;
  error: string[] | null;
  successMessage: string | null;

  // Actions
  setId: ReturnType<typeof vi.fn>;
  setName: ReturnType<typeof vi.fn>;
  setInitialName: ReturnType<typeof vi.fn>;
  setActive: ReturnType<typeof vi.fn>;
  setIsSaving: ReturnType<typeof vi.fn>;
  setError: ReturnType<typeof vi.fn>;
  setSuccessMessage: ReturnType<typeof vi.fn>;
  clearMessages: ReturnType<typeof vi.fn>;
  activate: ReturnType<typeof vi.fn>;
  deactivate: ReturnType<typeof vi.fn>;
  hasNameChanged: ReturnType<typeof vi.fn>;
  reset: ReturnType<typeof vi.fn>;
}

export function createMockWorkflowStore(
  overrides: Partial<MockWorkflowStoreState> = {},
): MockWorkflowStoreState {
  return {
    // Default state
    id: null,
    name: 'Untitled Workflow',
    isActive: false,
    initialName: null,
    isLoading: false,
    isSaving: false,
    isActivating: false,
    loadError: null,
    error: null,
    successMessage: null,

    // Default mock actions
    setId: vi.fn(),
    setName: vi.fn(),
    setInitialName: vi.fn(),
    setActive: vi.fn(),
    setIsSaving: vi.fn(),
    setError: vi.fn(),
    setSuccessMessage: vi.fn(),
    clearMessages: vi.fn(),
    activate: vi.fn().mockResolvedValue(true),
    deactivate: vi.fn().mockResolvedValue(true),
    hasNameChanged: vi.fn().mockReturnValue(false),
    reset: vi.fn(),

    // Apply overrides
    ...overrides,
  };
}

// ============================================================================
// UIStore Mock
// ============================================================================

export interface MockUIStoreState {
  // State
  showAppSelector: boolean;
  appSelectorType: 'trigger' | 'action';
  selectedPlaceholderId: string | null;
  dataFieldsModal: { nodeType: 'trigger' | 'action'; typeIdentifier: string } | null;
  nodeToDelete: { id: string; name: string } | null;
  showUnsavedDialog: boolean;

  // Actions
  openAppSelector: ReturnType<typeof vi.fn>;
  closeAppSelector: ReturnType<typeof vi.fn>;
  openDataFieldsModal: ReturnType<typeof vi.fn>;
  closeDataFieldsModal: ReturnType<typeof vi.fn>;
  requestDeleteNode: ReturnType<typeof vi.fn>;
  cancelDeleteNode: ReturnType<typeof vi.fn>;
  clearNodeToDelete: ReturnType<typeof vi.fn>;
  setShowUnsavedDialog: ReturnType<typeof vi.fn>;
  reset: ReturnType<typeof vi.fn>;
}

export function createMockUIStore(overrides: Partial<MockUIStoreState> = {}): MockUIStoreState {
  return {
    // Default state
    showAppSelector: false,
    appSelectorType: 'action',
    selectedPlaceholderId: null,
    dataFieldsModal: null,
    nodeToDelete: null,
    showUnsavedDialog: false,

    // Default mock actions
    openAppSelector: vi.fn(),
    closeAppSelector: vi.fn(),
    openDataFieldsModal: vi.fn(),
    closeDataFieldsModal: vi.fn(),
    requestDeleteNode: vi.fn(),
    cancelDeleteNode: vi.fn(),
    clearNodeToDelete: vi.fn(),
    setShowUnsavedDialog: vi.fn(),
    reset: vi.fn(),

    // Apply overrides
    ...overrides,
  };
}

// ============================================================================
// Store Mock Setup Helpers
// ============================================================================

/**
 * Creates a mock implementation for useWorkflowStore
 * Usage:
 *   vi.mocked(useWorkflowStore).mockImplementation(() => mockStore);
 */
export function setupWorkflowStoreMock(
  useWorkflowStore: ReturnType<typeof vi.fn>,
  overrides: Partial<MockWorkflowStoreState> = {},
) {
  const mockStore = createMockWorkflowStore(overrides);
  useWorkflowStore.mockImplementation((selector?: (state: MockWorkflowStoreState) => unknown) => {
    if (selector) {
      return selector(mockStore);
    }
    return mockStore;
  });
  return mockStore;
}

/**
 * Creates a mock implementation for useUIStore
 * Usage:
 *   vi.mocked(useUIStore).mockImplementation(() => mockStore);
 */
export function setupUIStoreMock(
  useUIStore: ReturnType<typeof vi.fn>,
  overrides: Partial<MockUIStoreState> = {},
) {
  const mockStore = createMockUIStore(overrides);
  useUIStore.mockImplementation((selector?: (state: MockUIStoreState) => unknown) => {
    if (selector) {
      return selector(mockStore);
    }
    return mockStore;
  });
  return mockStore;
}
