import type { LoginStatus } from "@/data/dto/auth";
import type { PaymentPlan } from "@/data/dto/payment";
import {
  getTipCoffeeOption,
  type TipCoffeeType,
} from "@/lib/tip/tip_coffee";

export function findTipCoffeePlan(
  plans: readonly PaymentPlan[],
  coffeeType: TipCoffeeType,
): PaymentPlan | null {
  const option = getTipCoffeeOption(coffeeType);
  return plans.find((plan) => plan.planId === option.planId) ?? null;
}

export function formatTipPrice(
  plan: PaymentPlan | null,
  coffeeType: TipCoffeeType,
): string {
  const option = getTipCoffeeOption(coffeeType);
  const amountCents = plan?.amountCents ?? option.amountCents;
  const currency = plan?.currency.trim().toUpperCase() || "USD";

  const amount = amountCents / 100;
  const formattedAmount = Number.isInteger(amount)
    ? String(amount)
    : amount.toFixed(2).replace(/\.?0+$/, "");

  if (currency === "USD") return `US$ ${formattedAmount}`;
  if (currency.length > 0) return `${currency} ${formattedAmount}`;
  return formattedAmount;
}

export function isRealLoginStatus(loginStatus: LoginStatus): boolean {
  return loginStatus !== "notLoggedIn" && loginStatus !== "guest";
}
