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

import { getDefaultPayChannelForCountryCode } from "../default_pay_channel";
import {
  canChoosePayChannel,
  getPaymentMethodConfig,
  resolvePayChannel,
} from "../payment_method";

describe("payment method rules", () => {
  afterEach(() => {
    vi.unstubAllEnvs();
  });

  it("defaults Philippines and Indonesia users to their regional Ezpay method", () => {
    expect(getDefaultPayChannelForCountryCode("PH")).toBe("ezpay");
    expect(getDefaultPayChannelForCountryCode("ph")).toBe("ezpay");
    expect(getDefaultPayChannelForCountryCode("ID")).toBe("ezpay");
    expect(getDefaultPayChannelForCountryCode(" id ")).toBe("ezpay");
    expect(getDefaultPayChannelForCountryCode("HK")).toBe("stripe");
    expect(getDefaultPayChannelForCountryCode(null)).toBe("stripe");
  });

  it("allows Philippines and Indonesia users to choose in production", () => {
    vi.stubEnv("NEXT_PUBLIC_APP_ENV", "production");

    expect(canChoosePayChannel("PH")).toBe(true);
    expect(canChoosePayChannel("ph")).toBe(true);
    expect(canChoosePayChannel("ID")).toBe(true);
    expect(canChoosePayChannel("HK")).toBe(false);
    expect(canChoosePayChannel(null)).toBe(false);
    expect(
      getPaymentMethodConfig({
        countryCode: null,
        requestedPayChannel: "ezpay",
      }),
    ).toEqual({
      canChoosePaymentMethod: false,
      initialPayChannel: "stripe",
      showPaymentMethodSelector: false,
      ezpayDisplayName: "GCash",
    });
  });

  it("resolves requested and default channels for Philippines", () => {
    vi.stubEnv("NEXT_PUBLIC_APP_ENV", "production");

    expect(
      resolvePayChannel({ countryCode: "PH", requestedPayChannel: null }),
    ).toBe("ezpay");
    expect(
      resolvePayChannel({
        countryCode: "PH",
        requestedPayChannel: "stripe",
      }),
    ).toBe("stripe");
  });

  it("defaults Indonesia to QRIS but permits an explicit Stripe choice", () => {
    vi.stubEnv("NEXT_PUBLIC_APP_ENV", "production");

    expect(
      getPaymentMethodConfig({
        countryCode: "ID",
        requestedPayChannel: null,
      }),
    ).toEqual({
      canChoosePaymentMethod: true,
      initialPayChannel: "ezpay",
      showPaymentMethodSelector: true,
      ezpayDisplayName: "QRIS",
    });
    expect(
      resolvePayChannel({ countryCode: "ID", requestedPayChannel: "stripe" }),
    ).toBe("stripe");
  });

  it("keeps test channels but shows the selector only for Philippines", () => {
    vi.stubEnv("NEXT_PUBLIC_APP_ENV", "test");

    expect(canChoosePayChannel(null)).toBe(true);
    expect(
      resolvePayChannel({
        countryCode: "HK",
        requestedPayChannel: "ezpay",
      }),
    ).toBe("ezpay");
    expect(
      getPaymentMethodConfig({
        countryCode: "HK",
        requestedPayChannel: "ezpay",
      }),
    ).toEqual({
      canChoosePaymentMethod: true,
      initialPayChannel: "ezpay",
      showPaymentMethodSelector: false,
      ezpayDisplayName: "GCash",
    });
    expect(
      getPaymentMethodConfig({
        countryCode: " ph ",
        requestedPayChannel: null,
      }).showPaymentMethodSelector,
    ).toBe(true);
    expect(
      getPaymentMethodConfig({
        countryCode: undefined,
        requestedPayChannel: null,
      }).showPaymentMethodSelector,
    ).toBe(false);
  });
});
