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

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

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

describe("getInsufficientCreditsMessageLimitView", () => {
  it("asks guests to log in for more free chats", () => {
    expect(getInsufficientCreditsMessageLimitView("guest")).toEqual({
      title: "Log in to get more free chats",
      description: "Free chats refresh every day.",
      ctaLabel: "Log in to continue",
      action: "auth",
    });
  });

  it("treats the transient not logged in state like guest", () => {
    expect(getInsufficientCreditsMessageLimitView("notLoggedIn")).toEqual({
      title: "Log in to get more free chats",
      description: "Free chats refresh every day.",
      ctaLabel: "Log in to continue",
      action: "auth",
    });
  });

  it.each(["email", "facebook", "google"] as const)(
    "asks %s users to top up credits",
    (loginStatus) => {
      expect(getInsufficientCreditsMessageLimitView(loginStatus)).toEqual({
        title: "Insufficient credits",
        description: "Free chats refresh every day.",
        ctaLabel: "Top up credits to continue",
        action: "topup",
      });
    },
  );
});

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);
  });
});
