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

import { ROUTES } from "@/router/routes";

import {
  resolveExternalEntryDestination,
  resolveExternalEntryPromotionType,
  resolveExternalEntryTarget,
  shouldBindExternalFacebookIdentity,
} from "../external_entry";

describe("external entry navigation", () => {
  it("defaults to chat", () => {
    expect(resolveExternalEntryTarget({})).toBe(ROUTES.chat);
  });

  it("resolves only canonical targets", () => {
    expect(resolveExternalEntryTarget({ target: "chat" })).toBe(ROUTES.chat);
    expect(resolveExternalEntryTarget({ target: "tip" })).toBe(ROUTES.tip);
    expect(resolveExternalEntryTarget({ target: "private-room" })).toBe(
      ROUTES.privateRoom,
    );
  });

  it("rejects removed aliases and unsupported targets", () => {
    expect(resolveExternalEntryTarget({ target: "tips" })).toBe(ROUTES.chat);
    expect(resolveExternalEntryTarget({ target: "coffee" })).toBe(ROUTES.chat);
    expect(resolveExternalEntryTarget({ target: "private_room" })).toBe(
      ROUTES.chat,
    );
    expect(resolveExternalEntryTarget({ target: "/tip" })).toBe(ROUTES.chat);
    expect(resolveExternalEntryTarget({ target: "sidebar" })).toBe(ROUTES.chat);
  });

  it("routes tip entries to the requested coffee type", () => {
    expect(
      resolveExternalEntryDestination({
        target: "tip",
        coffeeType: "medium",
      }),
    ).toBe("/tip?coffee_type=medium");
    expect(
      resolveExternalEntryDestination({
        target: "tip",
        coffeeType: "large",
      }),
    ).toBe("/tip?coffee_type=large");
  });

  it("defaults invalid tip coffee types to small", () => {
    expect(
      resolveExternalEntryDestination({
        target: "tip",
        coffeeType: "unknown",
      }),
    ).toBe("/tip?coffee_type=small");
  });
});

describe("external entry facebook identity binding", () => {
  it("binds only a ready real-user session with an external id", () => {
    expect(
      shouldBindExternalFacebookIdentity({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "email",
        asid: "asid-1",
      }),
    ).toBe(true);
    expect(
      shouldBindExternalFacebookIdentity({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "google",
        asid: "asid-1",
      }),
    ).toBe(true);
    expect(
      shouldBindExternalFacebookIdentity({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "facebook",
        psid: "psid-1",
      }),
    ).toBe(true);
  });

  it("skips guest, notLoggedIn, loading, and empty identity inputs", () => {
    expect(
      shouldBindExternalFacebookIdentity({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "guest",
        asid: "asid-1",
      }),
    ).toBe(false);
    expect(
      shouldBindExternalFacebookIdentity({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "notLoggedIn",
        psid: "psid-1",
      }),
    ).toBe(false);
    expect(
      shouldBindExternalFacebookIdentity({
        hasInitialized: true,
        isLoading: true,
        loginStatus: "google",
        asid: "asid-1",
      }),
    ).toBe(false);
    expect(
      shouldBindExternalFacebookIdentity({
        hasInitialized: true,
        isLoading: false,
        loginStatus: "google",
      }),
    ).toBe(false);
  });
});

describe("external entry promotion", () => {
  it("accepts only explicit promotion mode and supported message types", () => {
    expect(
      resolveExternalEntryPromotionType({
        mode: "promotion",
        promotionType: "voice",
      }),
    ).toBe("voice");
    expect(
      resolveExternalEntryPromotionType({
        mode: "promotion",
        promotionType: "image",
      }),
    ).toBe("image");
    expect(
      resolveExternalEntryPromotionType({
        mode: "normal",
        promotionType: "private",
      }),
    ).toBeNull();
    expect(
      resolveExternalEntryPromotionType({
        mode: "promotion",
        promotionType: "video",
      }),
    ).toBeNull();
  });
});
