/**
 * Converts a number to Persian words
 * @param num - The number to convert
 * @returns Persian words representation of the number
 */
export const convertNumberToPersian = (num: number | null | undefined): string => {
  if (num === null || num === undefined || isNaN(num)) {
    return "صفر";
  }

  if (num === 0) {
    return "صفر";
  }

  const ones = [
    "",
    "یک",
    "دو",
    "سه",
    "چهار",
    "پنج",
    "شش",
    "هفت",
    "هشت",
    "نه",
    "ده",
    "یازده",
    "دوازده",
    "سیزده",
    "چهارده",
    "پانزده",
    "شانزده",
    "هفده",
    "هجده",
    "نوزده",
  ];

  const tens = [
    "",
    "",
    "بیست",
    "سی",
    "چهل",
    "پنجاه",
    "شصت",
    "هفتاد",
    "هشتاد",
    "نود",
  ];

  const hundreds = [
    "",
    "صد",
    "دویست",
    "سیصد",
    "چهارصد",
    "پانصد",
    "ششصد",
    "هفتصد",
    "هشتصد",
    "نهصد",
  ];

  const convertHundreds = (n: number): string => {
    if (n === 0) return "";
    if (n < 20) return ones[n];
    if (n < 100) {
      const ten = Math.floor(n / 10);
      const one = n % 10;
      if (one === 0) {
        return tens[ten];
      }
      return `${tens[ten]} و ${ones[one]}`;
    }
    if (n < 1000) {
      const hundred = Math.floor(n / 100);
      const remainder = n % 100;
      if (remainder === 0) {
        return hundreds[hundred];
      }
      return `${hundreds[hundred]} و ${convertHundreds(remainder)}`;
    }
    return "";
  };

  const convertThousands = (n: number): string => {
    if (n < 1000) {
      return convertHundreds(n);
    }
    if (n < 1000000) {
      const thousand = Math.floor(n / 1000);
      const remainder = n % 1000;
      let result = "";
      
      if (thousand === 1) {
        result = "هزار";
      } else if (thousand < 1000) {
        result = `${convertHundreds(thousand)} هزار`;
      }
      
      if (remainder > 0) {
        result = result ? `${result} و ${convertHundreds(remainder)}` : convertHundreds(remainder);
      }
      return result;
    }
    if (n < 1000000000) {
      const million = Math.floor(n / 1000000);
      const remainder = n % 1000000;
      let result = "";
      
      if (million === 1) {
        result = "یک میلیون";
      } else if (million < 1000) {
        result = `${convertHundreds(million)} میلیون`;
      } else {
        result = `${convertThousands(million)} میلیون`;
      }
      
      if (remainder > 0) {
        result = `${result} و ${convertThousands(remainder)}`;
      }
      return result;
    }
    if (n < 1000000000000) {
      const billion = Math.floor(n / 1000000000);
      const remainder = n % 1000000000;
      let result = "";
      
      if (billion === 1) {
        result = "یک میلیارد";
      } else if (billion < 1000) {
        result = `${convertHundreds(billion)} میلیارد`;
      } else {
        result = `${convertThousands(billion)} میلیارد`;
      }
      
      if (remainder > 0) {
        result = `${result} و ${convertThousands(remainder)}`;
      }
      return result;
    }
    return "";
  };

  return convertThousands(Math.floor(num));
};

