/**
 * Mock implementations for Stripe modules used in testing.
 */
import { vi } from 'vitest';

// Mock Stripe instance
export const mockStripe = {
  confirmCardPayment: vi.fn().mockResolvedValue({
    paymentIntent: { status: 'succeeded' },
    error: null,
  }),
  confirmPayment: vi.fn().mockResolvedValue({
    paymentIntent: { status: 'succeeded' },
    error: null,
  }),
  elements: vi.fn().mockReturnValue({
    create: vi.fn().mockReturnValue({
      mount: vi.fn(),
      destroy: vi.fn(),
      on: vi.fn(),
      update: vi.fn(),
    }),
    getElement: vi.fn(),
  }),
  createPaymentMethod: vi.fn().mockResolvedValue({
    paymentMethod: { id: 'pm_test_123' },
    error: null,
  }),
};

// Mock loadStripe function from @stripe/stripe-js
export const loadStripe = vi.fn().mockResolvedValue(mockStripe);

// Mock Stripe Connect instance
export const mockStripeConnectInstance = {
  create: vi.fn().mockReturnValue({
    mount: vi.fn(),
    destroy: vi.fn(),
    update: vi.fn(),
  }),
};

// Mock loadConnectAndInitialize from @stripe/connect-js
export const loadConnectAndInitialize = vi.fn().mockReturnValue(mockStripeConnectInstance);

// Mock ConnectComponentsProvider from @stripe/react-connect-js
export const ConnectComponentsProvider = vi.fn(({ children }: { children: React.ReactNode }) => children);

// Mock ConnectAccountOnboarding from @stripe/react-connect-js
export const ConnectAccountOnboarding = vi.fn(({ onExit }: { onExit?: () => void }) => {
  return null;
});

// Helper to reset all Stripe mocks
export const resetStripeMocks = () => {
  mockStripe.confirmCardPayment.mockReset().mockResolvedValue({
    paymentIntent: { status: 'succeeded' },
    error: null,
  });
  mockStripe.confirmPayment.mockReset().mockResolvedValue({
    paymentIntent: { status: 'succeeded' },
    error: null,
  });
  loadStripe.mockReset().mockResolvedValue(mockStripe);
  loadConnectAndInitialize.mockReset().mockReturnValue(mockStripeConnectInstance);
};

// Helper to mock Stripe payment failure
export const mockStripePaymentFailure = (errorMessage: string) => {
  mockStripe.confirmCardPayment.mockResolvedValue({
    paymentIntent: null,
    error: { message: errorMessage },
  });
};

// Helper to mock Stripe 3DS authentication required
export const mockStripe3DSRequired = () => {
  mockStripe.confirmCardPayment.mockResolvedValue({
    paymentIntent: { status: 'requires_action' },
    error: null,
  });
};

export default mockStripe;
