import type { LoginStatus } from "@/data/dto/auth";
import type { PaymentPlan } from "@/data/dto/payment";

const TIP_COFFEE_PLAN_ID = "tip_coffee";
const TIP_COFFEE_ORDER_TYPE = "tip_coffee";

export function findTipCoffeePlan(
  plans: readonly PaymentPlan[],
): PaymentPlan | null {
  return (
    plans.find((plan) => plan.planId === TIP_COFFEE_PLAN_ID) ??
    plans.find((plan) => plan.orderType === TIP_COFFEE_ORDER_TYPE) ??
    null
  );
}

export function formatTipPrice(plan: PaymentPlan | null): string {
  if (!plan) return "US$ 5";

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

  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";
}
