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

import {
  GiftCategorySchema,
  GiftProductSchema,
} from "@/data/schemas/payment";

import {
  formatGiftPrice,
  getGiftImageSources,
  TIP_GIFT_PLACEHOLDER_IMAGE,
} from "../tip-screen.helpers";

const category = GiftCategorySchema.parse({
  category: "coffee",
  name: "Coffee",
  productCount: 1,
  imageUrl: "https://cdn.example.com/category.jpg",
});

const product = GiftProductSchema.parse({
  planId: "gift_1",
  planName: "Golden Reserve",
  orderType: "tip",
  tipType: "coffee_medium",
  category: "coffee",
  characterId: "elio",
  description: "A warm coffee",
  imageUrl: "https://cdn.example.com/product.jpg",
  amountCents: 999,
  currency: "USD",
  autoRenew: false,
  isFirstRechargeOffer: false,
  firstRechargeDiscountPercent: 0,
  promotionType: null,
});

describe("tip screen helpers", () => {
  it("formats prices from backend amount and currency", () => {
    expect(formatGiftPrice(999, "usd")).toBe("$9.99");
    expect(formatGiftPrice(500, "PHP")).toContain("5.00");
  });

  it("orders product, category, and local image fallbacks", () => {
    expect(getGiftImageSources(product, category)).toEqual([
      "https://cdn.example.com/product.jpg",
      "https://cdn.example.com/category.jpg",
      TIP_GIFT_PLACEHOLDER_IMAGE,
    ]);
  });

  it("always retains the local placeholder", () => {
    expect(
      getGiftImageSources(
        GiftProductSchema.parse({ ...product, imageUrl: null }),
        GiftCategorySchema.parse({ ...category, imageUrl: null }),
      ),
    ).toEqual([TIP_GIFT_PLACEHOLDER_IMAGE]);
  });

  it.each([
    ["coffee_small", "/images/tip/small.jpg"],
    ["coffee_medium", "/images/tip/medium.png"],
    ["coffee_large", "/images/tip/large.png"],
  ])("uses the %s tier image as its local fallback", (tipType, expected) => {
    const tierProduct = GiftProductSchema.parse({
      ...product,
      tipType,
      imageUrl: null,
    });
    const categoryWithoutImage = GiftCategorySchema.parse({
      ...category,
      imageUrl: null,
    });

    expect(getGiftImageSources(tierProduct, categoryWithoutImage)).toEqual([
      expected,
    ]);
  });

  it("uses the generic placeholder for unknown gift types", () => {
    const unknownProduct = GiftProductSchema.parse({
      ...product,
      tipType: "flowers",
      imageUrl: null,
    });

    expect(getGiftImageSources(unknownProduct, null)).toEqual([
      TIP_GIFT_PLACEHOLDER_IMAGE,
    ]);
  });
});
