import type { Position, TeamPlayer } from "@prisma/client";
import { FORMATIONS } from "@/lib/teamValidation";

export const SPECIAL_CARD_TEAM_LIMIT = 3;

export function getSpecialCardSalePrice(price: number) {
  return Math.round(price * 0.7);
}

export function getPositionLabel(position: Position | string) {
  switch (position) {
    case "GK":
      return "دروازه‌بان";
    case "DEF":
      return "مدافع";
    case "MID":
      return "هافبک";
    case "FWD":
      return "مهاجم";
    default:
      return position;
  }
}

export function getAutoPlacement(
  formation: string,
  teamPlayers: Array<TeamPlayer & { player: { position: Position } }>,
  position: Position
) {
  const fmt = FORMATIONS[formation] ?? FORMATIONS["4-3-3"];
  const starterLimit = position === "GK" ? 1 : fmt[position.toLowerCase() as "def" | "mid" | "fwd"];
  const starters = teamPlayers.filter((item) => !item.isBench && item.player.position === position);
  if (starters.length < starterLimit) {
    return { isBench: false as const, placementLabel: "فیکس" };
  }

  const bench = teamPlayers.filter((item) => item.isBench && item.player.position === position);
  if (bench.length < 1) {
    return { isBench: true as const, placementLabel: "ذخیره" };
  }

  return null;
}
