export interface IranianBank {
  name: string;
  prefix: string;
  color: {
    from: string;
    to: string;
  };
}

export const IRANIAN_BANKS: IranianBank[] = [
  { name: "اقتصاد نوین", prefix: "627412", color: { from: "#1e3a8a", to: "#3b82f6" } },
  { name: "توسعه صادرات ایران", prefix: "207177", color: { from: "#065f46", to: "#10b981" } },
  { name: "توسعه صادرات ایران", prefix: "627648", color: { from: "#065f46", to: "#10b981" } },
  { name: "انصار", prefix: "627381", color: { from: "#7c2d12", to: "#ea580c" } },
  { name: "پاسارگاد", prefix: "502229", color: { from: "#4c1d95", to: "#8b5cf6" } },
  { name: "پاسارگاد", prefix: "639347", color: { from: "#4c1d95", to: "#8b5cf6" } },
  { name: "ایران زمین", prefix: "505785", color: { from: "#831843", to: "#ec4899" } },
  { name: "شهر", prefix: "502806", color: { from: "#713f12", to: "#f59e0b" } },
  { name: "پارسیان", prefix: "622106", color: { from: "#1e40af", to: "#60a5fa" } },
  { name: "پارسیان", prefix: "639194", color: { from: "#1e40af", to: "#60a5fa" } },
  { name: "پارسیان", prefix: "627884", color: { from: "#1e40af", to: "#60a5fa" } },
  { name: "توسعه تعاون", prefix: "502908", color: { from: "#0f766e", to: "#14b8a6" } },
  { name: "کارآفرین", prefix: "502910", color: { from: "#be123c", to: "#f43f5e" } },
  { name: "کارآفرین", prefix: "627488", color: { from: "#be123c", to: "#f43f5e" } },
  { name: "دی", prefix: "502938", color: { from: "#1e293b", to: "#475569" } },
  { name: "گردشگری", prefix: "505416", color: { from: "#7c3aed", to: "#a78bfa" } },
  { name: "آینده", prefix: "636214", color: { from: "#b91c1c", to: "#ef4444" } },
  { name: "موسسه اعتباری کوثر", prefix: "505801", color: { from: "#0369a1", to: "#0ea5e9" } },
  { name: "تجارت", prefix: "627353", color: { from: "#15803d", to: "#22c55e" } },
  { name: "سپه", prefix: "589210", color: { from: "#1e40af", to: "#3b82f6" } },
  { name: "رفاه کارگران", prefix: "589463", color: { from: "#9f1239", to: "#fb7185" } },
  { name: "صادرات ایران", prefix: "603769", color: { from: "#065f46", to: "#34d399" } },
  { name: "کشاورزی", prefix: "603770", color: { from: "#15803d", to: "#4ade80" } },
  { name: "کشاورزی", prefix: "639217", color: { from: "#15803d", to: "#4ade80" } },
  { name: "ملی ایران", prefix: "603799", color: { from: "#1e3a8a", to: "#60a5fa" } },
  { name: "قرض الحسنه مهر ایران", prefix: "606373", color: { from: "#854d0e", to: "#fbbf24" } },
  { name: "ملت", prefix: "610433", color: { from: "#b91c1c", to: "#f87171" } },
  { name: "ملت", prefix: "991975", color: { from: "#b91c1c", to: "#f87171" } },
  { name: "سامان", prefix: "621986", color: { from: "#1e40af", to: "#3b82f6" } },
  { name: "سرمایه", prefix: "639607", color: { from: "#4c1d95", to: "#a78bfa" } },
  { name: "سینا", prefix: "639346", color: { from: "#0f766e", to: "#2dd4bf" } },
  { name: "صنعت و معدن", prefix: "627961", color: { from: "#831843", to: "#f472b6" } },
  { name: "قوامین", prefix: "639599", color: { from: "#713f12", to: "#fb923c" } },
  { name: "پست ایران", prefix: "627760", color: { from: "#fbbf24", to: "#fde047" } },
  { name: "مسکن", prefix: "628023", color: { from: "#0891b2", to: "#22d3ee" } },
  { name: "موسسه اعتباری توسعه", prefix: "628157", color: { from: "#7c2d12", to: "#f97316" } },
  { name: "مرکزی", prefix: "636795", color: { from: "#1e293b", to: "#64748b" } },
  { name: "حکمت ایرانیان", prefix: "636949", color: { from: "#4c1d95", to: "#8b5cf6" } },
  { name: "مهر اقتصاد", prefix: "639370", color: { from: "#be123c", to: "#fb7185" } },
];

/**
 * Detect Iranian bank from card number
 * @param cardNumber - Card number (can be partial or full, with or without dashes)
 * @returns Detected bank or null
 */
export function detectIranianBank(cardNumber: string): IranianBank | null {
  // Remove all non-digit characters
  const cleanCardNumber = cardNumber.replace(/\D/g, "");

  // Need at least 6 digits to detect
  if (cleanCardNumber.length < 6) {
    return null;
  }

  // Check against all bank prefixes
  for (const bank of IRANIAN_BANKS) {
    if (cleanCardNumber.startsWith(bank.prefix)) {
      return bank;
    }
  }

  return null;
}

/**
 * Validate if card number belongs to an Iranian bank
 * @param cardNumber - Full card number (16 digits)
 * @returns true if valid Iranian bank card
 */
export function validateIranianBankCard(cardNumber: string): boolean {
  const cleanCardNumber = cardNumber.replace(/\D/g, "");

  // Must be exactly 16 digits
  if (cleanCardNumber.length !== 16) {
    return false;
  }

  // Must match a known bank prefix
  return detectIranianBank(cleanCardNumber) !== null;
}
