import { afterEach, describe, expect, it, vi } from "vitest";

import {
  getInsufficientCreditsMessageLimitView,
  shouldStartExternalBrowserPrompt,
  shouldShowMessageLimitBanner,
} from "../chat-screen.helpers";

afterEach(() => {
  vi.unstubAllEnvs();
});

describe("getInsufficientCreditsMessageLimitView", () => {
  it("shows the same top-up prompt without checking login status", () => {
    expect(getInsufficientCreditsMessageLimitView()).toEqual({
      title: "Out of Daily Free Chats",
      description: "",
      ctaLabel: "Top up credits to continue",
    });
  });
});

describe("shouldShowMessageLimitBanner", () => {
  it("shows only for visible insufficient credit prompts", () => {
    expect(
      shouldShowMessageLimitBanner({
        upgradePromptVisible: true,
        upgradeReason: "insufficient_credits",
      }),
    ).toBe(true);
  });

  it("hides when the prompt is not visible", () => {
    expect(
      shouldShowMessageLimitBanner({
        upgradePromptVisible: false,
        upgradeReason: "insufficient_credits",
      }),
    ).toBe(false);
  });

  it("hides when there is no upgrade reason", () => {
    expect(
      shouldShowMessageLimitBanner({
        upgradePromptVisible: true,
        upgradeReason: null,
      }),
    ).toBe(false);
  });
});

describe("shouldStartExternalBrowserPrompt", () => {
  it("shows for facebook users inside an in-app browser", () => {
    vi.stubEnv("NEXT_PUBLIC_APP_ENV", "production");

    expect(
      shouldStartExternalBrowserPrompt({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "facebook",
        isInAppBrowser: true,
      }),
    ).toBe(true);
  });

  it.each(["guest", "notLoggedIn", "email", "google"] as const)(
    "hides for %s users even inside an in-app browser",
    (loginStatus) => {
      vi.stubEnv("NEXT_PUBLIC_APP_ENV", "production");

      expect(
        shouldStartExternalBrowserPrompt({
          hasInitialized: true,
          isLoading: false,
          loginStatus,
          isInAppBrowser: true,
        }),
      ).toBe(false);
    },
  );

  it("hides for facebook users in normal browsers", () => {
    vi.stubEnv("NEXT_PUBLIC_APP_ENV", "production");

    expect(
      shouldStartExternalBrowserPrompt({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "facebook",
        isInAppBrowser: false,
      }),
    ).toBe(false);
  });

  it("shows in development for easier testing", () => {
    vi.stubEnv("NEXT_PUBLIC_APP_ENV", "development");

    expect(
      shouldStartExternalBrowserPrompt({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "guest",
        isInAppBrowser: false,
      }),
    ).toBe(true);
  });

  it("waits for auth initialization", () => {
    vi.stubEnv("NEXT_PUBLIC_APP_ENV", "production");

    expect(
      shouldStartExternalBrowserPrompt({
        hasInitialized: false,
        isLoading: false,
        loginStatus: "facebook",
        isInAppBrowser: true,
      }),
    ).toBe(false);
  });
});
