/**
 * GCC-specific Character Normalization Rules
 *
 * Pronunciation and formatting rules for Arabic Eastern numerals,
 * GCC currencies, Hijri dates, and regional phone prefixes.
 */

import type {
  CharacterNormalization,
  GCCCurrency,
} from "@/lib/types/voice-agent.types";

/** Currency display names for voice pronunciation */
export const GCC_CURRENCY_NAMES: Record<
  GCCCurrency,
  { en: string; ar: string; symbol: string }
> = {
  QAR: { en: "Qatari Riyal", ar: "ريال قطري", symbol: "ر.ق" },
  SAR: { en: "Saudi Riyal", ar: "ريال سعودي", symbol: "ر.س" },
  AED: { en: "UAE Dirham", ar: "درهم إماراتي", symbol: "د.إ" },
  KWD: { en: "Kuwaiti Dinar", ar: "دينار كويتي", symbol: "د.ك" },
  BHD: { en: "Bahraini Dinar", ar: "دينار بحريني", symbol: "د.ب" },
  OMR: { en: "Omani Rial", ar: "ريال عماني", symbol: "ر.ع" },
};

/** Country calling codes for GCC countries */
export const GCC_PHONE_PREFIXES: Record<
  string,
  { code: string; country: string }
> = {
  QA: { code: "+974", country: "Qatar" },
  SA: { code: "+966", country: "Saudi Arabia" },
  AE: { code: "+971", country: "UAE" },
  KW: { code: "+965", country: "Kuwait" },
  BH: { code: "+973", country: "Bahrain" },
  OM: { code: "+968", country: "Oman" },
};

/** GCC timezone mappings */
export const GCC_TIMEZONES: Record<string, string> = {
  QA: "Asia/Qatar",
  SA: "Asia/Riyadh",
  AE: "Asia/Dubai",
  KW: "Asia/Kuwait",
  BH: "Asia/Bahrain",
  OM: "Asia/Muscat",
};

/**
 * Build character normalization prompt section for the voice agent.
 * Instructs the agent how to pronounce dates, numbers, currencies, and phone numbers.
 */
export function buildNormalizationPrompt(
  normalization: CharacterNormalization,
): string {
  const rules: string[] = [];

  // Date format
  if (normalization.dateFormat === "arabic") {
    rules.push(
      'Dates: Use Arabic/Hijri calendar when speaking Arabic. Say dates in words, e.g. "الخامس من فبراير" not "٥/٢".',
    );
  } else if (normalization.dateFormat === "western") {
    rules.push(
      'Dates: Always use the Gregorian calendar. Say dates in words, e.g. "February fifth" not "02/05".',
    );
  } else {
    rules.push(
      "Dates: Say dates in words. Use Gregorian in English and either format in Arabic based on context.",
    );
  }

  // Number format
  if (normalization.numberFormat === "arabic_eastern") {
    rules.push(
      "Numbers: When speaking Arabic, use Eastern Arabic numerals pronunciation (واحد، اثنان، ثلاثة). Spell out numbers when possible.",
    );
  } else if (normalization.numberFormat === "arabic_western") {
    rules.push(
      "Numbers: Use standard Western numeral pronunciation in both languages. Spell out numbers when possible.",
    );
  } else {
    rules.push(
      "Numbers: Spell out numbers in the spoken language. Adapt numeral style to the conversation language.",
    );
  }

  // Currency format
  if (normalization.currencyFormat && normalization.currencyFormat !== "auto") {
    const currency = GCC_CURRENCY_NAMES[normalization.currencyFormat];
    rules.push(
      `Currency: Express amounts in ${currency.en} (${currency.ar}). Say "one hundred ${currency.en.split(" ").pop()}" not "100 ${normalization.currencyFormat}".`,
    );
  } else {
    rules.push(
      "Currency: Express amounts in the local currency. Always say the currency name, not the code.",
    );
  }

  // Phone format
  if (normalization.phoneFormat === "local") {
    rules.push(
      "Phone numbers: Read phone numbers in local format without country code. Group digits naturally.",
    );
  } else {
    rules.push(
      "Phone numbers: Read phone numbers with country code. Group digits in pairs or triplets for clarity.",
    );
  }

  return rules.join("\n");
}

/**
 * Default normalization settings for a GCC-based business.
 */
export const DEFAULT_GCC_NORMALIZATION: CharacterNormalization = {
  dateFormat: "auto",
  currencyFormat: "auto",
  phoneFormat: "international",
  numberFormat: "auto",
};
