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

import { PaymentPlan } from "@/data/dto/payment";
import type { PaymentPlanInput } from "@/data/schemas/payment/payment_plan";

import {
  findTipCoffeePlan,
  formatTipPrice,
  isRealLoginStatus,
} from "../tip-screen.helpers";

function makePlan(input: Partial<PaymentPlanInput>): PaymentPlan {
  return PaymentPlan.from({
    planId: "coins_100",
    planName: "Coins",
    orderType: "dol",
    vipDays: null,
    dolAmount: 100,
    creditBalance: 100,
    amountCents: 990,
    originalAmountCents: null,
    dailyPriceCents: null,
    currency: "USD",
    isFirstRechargeOffer: false,
    firstRechargeDiscountPercent: null,
    promotionType: null,
    ...input,
  });
}

describe("tip screen helpers", () => {
  it("matches the official small coffee plan", () => {
    const plan = makePlan({
      planId: "tip_coffee_usd_4_99",
    });

    expect(findTipCoffeePlan([makePlan({}), plan], "small")).toBe(plan);
  });

  it("matches medium and large coffee plans independently", () => {
    const medium = makePlan({
      planId: "tip_coffee_usd_9_99",
      orderType: "tip",
      amountCents: 999,
    });
    const large = makePlan({
      planId: "tip_coffee_usd_19_99",
      orderType: "tip",
      amountCents: 1999,
    });

    expect(findTipCoffeePlan([large, medium], "medium")).toBe(medium);
    expect(findTipCoffeePlan([medium, large], "large")).toBe(large);
  });

  it("does not infer a coffee tier from order type or amount", () => {
    const ambiguousPlan = makePlan({
      planId: "legacy_coffee",
      orderType: "tip",
      amountCents: 999,
    });

    expect(findTipCoffeePlan([ambiguousPlan], "medium")).toBeNull();
  });

  it("formats USD prices with the product-style label", () => {
    expect(
      formatTipPrice(makePlan({ amountCents: 500, currency: "USD" }), "small"),
    ).toBe("US$ 5");
  });

  it("uses the selected coffee price when its plan is unavailable", () => {
    expect(formatTipPrice(null, "small")).toBe("US$ 4.99");
    expect(formatTipPrice(null, "medium")).toBe("US$ 9.99");
    expect(formatTipPrice(null, "large")).toBe("US$ 19.99");
  });

  it("treats guest and notLoggedIn as non-real login states", () => {
    expect(isRealLoginStatus("guest")).toBe(false);
    expect(isRealLoginStatus("notLoggedIn")).toBe(false);
    expect(isRealLoginStatus("facebook")).toBe(true);
  });
});
