import { beforeEach, describe, expect, test, vi } from "vitest";

// Mock the (server-only) constants module so the tests are environment-independent. Getters let each
// test flip the flags at runtime — the utility reads them through live import bindings at call time.
const constantsOverrides = vi.hoisted(() => ({
  IS_FORMBRICKS_CLOUD: true,
  SIGNUP_DOMAIN_CHECK_ON_INVITES: false,
}));

vi.mock("@/lib/constants", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@/lib/constants")>();
  return {
    ...actual,
    get IS_FORMBRICKS_CLOUD() {
      return constantsOverrides.IS_FORMBRICKS_CLOUD;
    },
    get SIGNUP_DOMAIN_CHECK_ON_INVITES() {
      return constantsOverrides.SIGNUP_DOMAIN_CHECK_ON_INVITES;
    },
  };
});

const { isBlockedEmailDomain, isSignupEmailDomainBlocked } = await import("./signup-email-domain");

beforeEach(() => {
  constantsOverrides.IS_FORMBRICKS_CLOUD = true;
  constantsOverrides.SIGNUP_DOMAIN_CHECK_ON_INVITES = false;
});

describe("isBlockedEmailDomain", () => {
  test("returns true for known free / personal providers", () => {
    for (const email of [
      "test@gmail.com",
      "test@googlemail.com",
      "test@yahoo.com",
      "test@hotmail.com",
      "test@outlook.com",
      "test@icloud.com",
      "test@protonmail.com",
      "test@proton.me",
      "test@gmx.com",
      "test@yandex.com",
      "test@aol.com",
      "test@protonmail.ch",
      "test@passinbox.com",
      "test@passmail.net",
      "test@passfwd.com",
      "test@simplelogin.com",
      "test@simplelogin.fr",
      "test@aleeas.com",
      "test@slmail.me",
      "test@silomails.com",
      "test@8shield.net",
      "test@dralias.com",
      "test@oemails.com",
      "test@consultant.com",
      "test@fastmail.com",
      "test@live.nl",
      "test@yahoo.fr",
      "test@privaterelay.appleid.com",
      "test@private.icloud.com",
      "test@anonaddy.com",
      "test@8alias.com",
      "test@keemail.me",
      "test@tuta.io",
      "test@tuta.com",
      "test@tutanota.de",
      "test@tutamail.com",
      "test@ik.me",
      "test@murena.io",
      "test@firemail.cc",
      "test@cock.li",
      "test@tmpmailtor.com",
      "test@allwebemails.com",
      "test@zohomail.com",
      "test@null.net",
      "test@139.com",
      "test@orange.fr",
      "test@centrum.cz",
      "test@wp.pl",
      "test@earthlink.net",
      "test@roadrunner.com",
      "test@gmail.cz",
      "test@sina.com",
      "test@sohu.com",
      "test@aliyun.com",
      "test@mail.ru",
      "test@web.de",
      "test@t-online.de",
      "test@naver.com",
      "test@daum.net",
      "test@tutanota.com",
      "test@hushmail.com",
    ]) {
      expect(isBlockedEmailDomain(email)).toBe(true);
    }
  });

  test("returns true for known disposable / burner providers", () => {
    for (const email of [
      "test@mailinator.com",
      "test@guerrillamail.com",
      "test@yopmail.com",
      "test@10minutemail.com",
      "test@sharklasers.com",
      "test@33mail.com",
    ]) {
      expect(isBlockedEmailDomain(email)).toBe(true);
    }
  });

  test("blocks any subdomain of a wildcard disposable provider", () => {
    // 33mail and similar give each user their own subdomain, so the root alone is not enough.
    expect(isBlockedEmailDomain("jerry90@jerrygroup.33mail.com")).toBe(true);
    expect(isBlockedEmailDomain("a@deep.nested.33mail.com")).toBe(true);
    expect(isBlockedEmailDomain("A@JerryGroup.33Mail.com.")).toBe(true);
    // Wildcard roots missing from the exact list are still blocked themselves.
    expect(isBlockedEmailDomain("test@anonaddy.com")).toBe(true);
    // A lookalike that only ends in the same characters is not a subdomain.
    expect(isBlockedEmailDomain("test@not33mail.com")).toBe(false);
  });

  test("does a bounded number of lookups for a domain with many labels", () => {
    // Better Auth's sign-up route doesn't cap the email length, so one lookup per label would let a
    // single request hash ~200k suffixes. Counting lookups keeps this deterministic, unlike a timer.
    const longDomain = "a.".repeat(200_000);
    const hasSpy = vi.spyOn(Set.prototype, "has");
    try {
      expect(isBlockedEmailDomain(`x@${longDomain}com`)).toBe(false);
      expect(isBlockedEmailDomain(`x@${longDomain}33mail.com`)).toBe(true);
      expect(hasSpy.mock.calls.length).toBeLessThan(20);
    } finally {
      hasSpy.mockRestore();
    }
  });

  test("returns false for company domains", () => {
    for (const email of [
      "alice@acme-corp.com",
      "bob@formbricks.com",
      "carol@stripe.com",
      "dave@my-company.co.uk",
    ]) {
      expect(isBlockedEmailDomain(email)).toBe(false);
    }
  });

  test("is case- and whitespace-insensitive", () => {
    expect(isBlockedEmailDomain("Test@Gmail.Com")).toBe(true);
    expect(isBlockedEmailDomain("  TEST@YAHOO.COM  ")).toBe(true);
    expect(isBlockedEmailDomain("carol@STRIPE.com")).toBe(false);
  });

  test("matches on the domain after the last @", () => {
    // Outside the wildcard providers, only an exact domain match counts: a subdomain is NOT blocked.
    expect(isBlockedEmailDomain("user@mail.acme-corp.com")).toBe(false);
    expect(isBlockedEmailDomain("user@sub.gmail.com")).toBe(false);
  });

  test("blocks plus-addressed and trailing-dot variants of a blocked domain", () => {
    // Plus-addressing is a common evasion attempt; the domain is unchanged so it's still blocked.
    expect(isBlockedEmailDomain("user+tag@gmail.com")).toBe(true);
    // A trailing FQDN dot is stripped so it can't slip past the Set (SSO emails aren't zod-validated).
    expect(isBlockedEmailDomain("user@gmail.com.")).toBe(true);
    // Multiple trailing dots are all stripped (the linear scan matches the old /\.+$/ behavior).
    expect(isBlockedEmailDomain("user@gmail.com..")).toBe(true);
  });

  test("treats input with no usable domain as not-blocked", () => {
    // Missing/absent domain or local part → not blocked; an unexpected null/undefined caller degrades
    // to "allow" rather than throwing. Genuinely malformed addresses (e.g. double "@") are rejected by
    // email-format validation upstream before this predicate is reached.
    for (const email of ["", "notanemail", "@gmail.com", "test@", null, undefined]) {
      expect(isBlockedEmailDomain(email as unknown as string)).toBe(false);
    }
  });
});

describe("isSignupEmailDomainBlocked", () => {
  const noInvite = () => Promise.resolve(false);
  const validInvite = () => Promise.resolve(true);

  test("blocks a personal domain on Cloud when there is no matching invite", async () => {
    expect(await isSignupEmailDomainBlocked("user@gmail.com", noInvite)).toBe(true);
  });

  test("allows a company domain", async () => {
    expect(await isSignupEmailDomainBlocked("user@acme-corp.com", noInvite)).toBe(false);
  });

  test("exempts a valid matching invite by default", async () => {
    expect(await isSignupEmailDomainBlocked("user@gmail.com", validInvite)).toBe(false);
  });

  test("still blocks a personal-domain invite when the kill-switch is enabled", async () => {
    constantsOverrides.SIGNUP_DOMAIN_CHECK_ON_INVITES = true;
    expect(await isSignupEmailDomainBlocked("user@gmail.com", validInvite)).toBe(true);
  });

  test("never blocks when not on Formbricks Cloud", async () => {
    constantsOverrides.IS_FORMBRICKS_CLOUD = false;
    expect(await isSignupEmailDomainBlocked("user@gmail.com", noInvite)).toBe(false);
    expect(await isSignupEmailDomainBlocked("user@mailinator.com", validInvite)).toBe(false);
  });

  test("does not run the invite check when the domain is not blocked", async () => {
    const inviteCheck = vi.fn().mockResolvedValue(false);
    expect(await isSignupEmailDomainBlocked("user@acme-corp.com", inviteCheck)).toBe(false);
    expect(inviteCheck).not.toHaveBeenCalled();
  });

  test("does not run the invite check when not on Cloud", async () => {
    constantsOverrides.IS_FORMBRICKS_CLOUD = false;
    const inviteCheck = vi.fn().mockResolvedValue(true);
    expect(await isSignupEmailDomainBlocked("user@gmail.com", inviteCheck)).toBe(false);
    expect(inviteCheck).not.toHaveBeenCalled();
  });

  test("does not run the invite check when the kill-switch is enabled", async () => {
    constantsOverrides.SIGNUP_DOMAIN_CHECK_ON_INVITES = true;
    const inviteCheck = vi.fn().mockResolvedValue(true);
    expect(await isSignupEmailDomainBlocked("user@gmail.com", inviteCheck)).toBe(true);
    expect(inviteCheck).not.toHaveBeenCalled();
  });
});
