import type { Page } from "@playwright/test";

import { registerAuthMocks } from "./api/auth";
import { registerChatMocks } from "./api/chat";
import { registerCharacterMocks } from "./api/character";
import { registerPaymentMocks } from "./api/payment";
import { registerUserMocks } from "./api/user";

export interface MockCoreApisOptions {
  chatLimitTriggerAt?: number;
  chatSendTokenRefreshFlow?: boolean;
  paidImageFlow?: boolean;
  paidImageInsufficientCreditsFlow?: boolean;
  paidVoiceInsufficientCreditsFlow?: boolean;
  topUpHandoffFlow?: boolean;
  checkoutHandoffFlow?: boolean;
}

export async function mockCoreApis(page: Page, options: MockCoreApisOptions = {}) {
  const chatOptions = {
    chatLimitTriggerAt: options.chatLimitTriggerAt,
    chatSendTokenRefreshFlow: options.chatSendTokenRefreshFlow ?? false,
    paidImageFlow: options.paidImageFlow ?? false,
    paidImageInsufficientCreditsFlow: options.paidImageInsufficientCreditsFlow ?? false,
    paidVoiceInsufficientCreditsFlow: options.paidVoiceInsufficientCreditsFlow ?? false,
    topUpHandoffFlow: options.topUpHandoffFlow ?? false,
    checkoutHandoffFlow: options.checkoutHandoffFlow ?? false,
  };
  const chatState = { hasExpiredChatSend: false, paidImageRequested: false, paidImageUnlocked: false };

  await registerAuthMocks(page, {
    chatSendTokenRefreshFlow: chatOptions.chatSendTokenRefreshFlow,
    isChatSendTokenExpired: () => chatState.hasExpiredChatSend,
    topUpHandoffFlow: chatOptions.topUpHandoffFlow,
    checkoutHandoffFlow: chatOptions.checkoutHandoffFlow,
  });
  await registerCharacterMocks(page);
  await registerUserMocks(page);
  await registerChatMocks(page, chatOptions, chatState);
  await registerPaymentMocks(page);
}
