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

import {
  buildPendingPaymentSubscriptionUrl,
  clearPendingPaymentOrder,
  getPendingPaymentOrder,
  savePendingEzpayOrder,
} from "../pending_payment_order";

describe("pending payment order helpers", () => {
  it("keeps the pay channel when rebuilding subscription return urls", () => {
    expect(
      buildPendingPaymentSubscriptionUrl({
        payChannel: "ezpay",
        returnTo: "chat",
        subscriptionType: "topup",
      }),
    ).toBe(
      "/subscription?type=topup&payChannel=ezpay&paymentReturn=1&returnTo=chat&character=elio",
    );
  });

  it("preserves private zone returns when rebuilding Ezpay urls", () => {
    expect(
      buildPendingPaymentSubscriptionUrl({
        payChannel: "ezpay",
        returnTo: "private-zone",
        subscriptionType: "topup",
      }),
    ).toBe(
      "/subscription?type=topup&payChannel=ezpay&paymentReturn=1&returnTo=private-zone&character=elio",
    );
  });

  it("stores private zone as an Ezpay return target", async () => {
    await clearPendingPaymentOrder();

    const saveResult = await savePendingEzpayOrder({
      orderId: "order-private-zone",
      returnTo: "private-zone",
      subscriptionType: "topup",
      createdAt: 1,
    });
    const storedResult = await getPendingPaymentOrder();

    expect(saveResult.success).toBe(true);
    expect(storedResult).toMatchObject({
      success: true,
      data: {
        orderId: "order-private-zone",
        returnTo: "private-zone",
      },
    });
    await clearPendingPaymentOrder();
  });

  it("stores and rebuilds global profile returns", async () => {
    await clearPendingPaymentOrder();

    const saveResult = await savePendingEzpayOrder({
      orderId: "order-profile",
      returnTo: "profile",
      subscriptionType: "topup",
      characterSlug: "maya",
      createdAt: 1,
    });
    const storedResult = await getPendingPaymentOrder();

    expect(saveResult.success).toBe(true);
    expect(storedResult).toMatchObject({
      success: true,
      data: {
        orderId: "order-profile",
        returnTo: "profile",
        characterSlug: "maya",
      },
    });
    expect(
      buildPendingPaymentSubscriptionUrl({
        payChannel: "ezpay",
        returnTo: "profile",
        subscriptionType: "topup",
        characterSlug: "maya",
      }),
    ).toBe(
      "/subscription?type=topup&payChannel=ezpay&paymentReturn=1&returnTo=profile&character=maya",
    );
    await clearPendingPaymentOrder();
  });

  it("routes tip payments back with dynamic category and plan id", () => {
    expect(
      buildPendingPaymentSubscriptionUrl({
        payChannel: "ezpay",
        subscriptionType: "tip",
        giftCategory: "coffee",
        giftPlanId: "tip_coffee_usd_19_99",
      }),
    ).toBe(
      "/characters/elio/tip?category=coffee&planId=tip_coffee_usd_19_99&payChannel=ezpay&paymentReturn=1",
    );
  });

  it("lets legacy tip records fall back to the first backend product", () => {
    expect(
      buildPendingPaymentSubscriptionUrl({
        payChannel: "ezpay",
        subscriptionType: "tip",
      }),
    ).toBe(
      "/characters/elio/tip?payChannel=ezpay&paymentReturn=1",
    );
  });

  it("restores the character that started an Ezpay tip", () => {
    expect(
      buildPendingPaymentSubscriptionUrl({
        payChannel: "ezpay",
        subscriptionType: "tip",
        giftCategory: "coffee",
        giftPlanId: "tip_coffee_usd_4_99",
        characterSlug: "nayeli",
      }),
    ).toBe(
      "/characters/nayeli/tip?category=coffee&planId=tip_coffee_usd_4_99&payChannel=ezpay&paymentReturn=1",
    );
  });
});
