"""
Currency utilities for billing
Maps countries to their currencies based on Stripe/PayPal standards
"""

# Country to currency mapping (Stripe/PayPal standard format)
COUNTRY_CURRENCY_MAP = {
    # North America
    'US': 'USD',
    'CA': 'CAD',
    'MX': 'MXN',
    
    # Europe
    'GB': 'GBP',
    'DE': 'EUR',
    'FR': 'EUR',
    'IT': 'EUR',
    'ES': 'EUR',
    'NL': 'EUR',
    'BE': 'EUR',
    'AT': 'EUR',
    'PT': 'EUR',
    'IE': 'EUR',
    'GR': 'EUR',
    'FI': 'EUR',
    'LU': 'EUR',
    'CH': 'CHF',
    'NO': 'NOK',
    'SE': 'SEK',
    'DK': 'DKK',
    'PL': 'PLN',
    'CZ': 'CZK',
    'HU': 'HUF',
    'RO': 'RON',
    
    # Asia Pacific
    'IN': 'INR',
    'PK': 'PKR',
    'BD': 'BDT',
    'LK': 'LKR',
    'JP': 'JPY',
    'CN': 'CNY',
    'HK': 'HKD',
    'SG': 'SGD',
    'MY': 'MYR',
    'TH': 'THB',
    'ID': 'IDR',
    'PH': 'PHP',
    'VN': 'VND',
    'KR': 'KRW',
    'TW': 'TWD',
    'AU': 'AUD',
    'NZ': 'NZD',
    
    # Middle East
    'AE': 'AED',
    'SA': 'SAR',
    'QA': 'QAR',
    'KW': 'KWD',
    'BH': 'BHD',
    'OM': 'OMR',
    'IL': 'ILS',
    'TR': 'TRY',
    
    # Africa
    'ZA': 'ZAR',
    'NG': 'NGN',
    'KE': 'KES',
    'EG': 'EGP',
    'MA': 'MAD',
    
    # South America
    'BR': 'BRL',
    'AR': 'ARS',
    'CL': 'CLP',
    'CO': 'COP',
    'PE': 'PEN',
}

# Default currency fallback
DEFAULT_CURRENCY = 'USD'


def get_currency_for_country(country_code: str) -> str:
    """
    Get currency code for a given country code.
    
    Args:
        country_code: ISO 2-letter country code (e.g., 'US', 'GB', 'IN')
    
    Returns:
        Currency code (e.g., 'USD', 'GBP', 'INR')
    """
    if not country_code:
        return DEFAULT_CURRENCY
    
    country_code = country_code.upper().strip()
    return COUNTRY_CURRENCY_MAP.get(country_code, DEFAULT_CURRENCY)


def get_currency_symbol(currency_code: str) -> str:
    """
    Get currency symbol for a given currency code.
    
    Args:
        currency_code: Currency code (e.g., 'USD', 'GBP', 'INR')
    
    Returns:
        Currency symbol (e.g., '$', '£', '₹')
    """
    CURRENCY_SYMBOLS = {
        'USD': '$',
        'EUR': '€',
        'GBP': '£',
        'INR': '₹',
        'JPY': '¥',
        'CNY': '¥',
        'AUD': 'A$',
        'CAD': 'C$',
        'CHF': 'Fr',
        'SEK': 'kr',
        'NOK': 'kr',
        'DKK': 'kr',
        'PLN': 'zł',
        'BRL': 'R$',
        'ZAR': 'R',
        'AED': 'د.إ',
        'SAR': 'ر.س',
        'PKR': '₨',
    }
    
    return CURRENCY_SYMBOLS.get(currency_code, currency_code + ' ')


# Currency multipliers for countries with payment methods configured
# These represent approximate exchange rates to USD
CURRENCY_MULTIPLIERS = {
    'USD': 1.0,      # United States
    'GBP': 0.79,     # United Kingdom
    'INR': 83.0,     # India
    'PKR': 278.0,    # Pakistan
    'CAD': 1.35,     # Canada
    'AUD': 1.52,     # Australia
    'EUR': 0.92,     # Germany, France (Eurozone)
}

# Map countries to their multipliers
COUNTRY_MULTIPLIERS = {
    'US': CURRENCY_MULTIPLIERS['USD'],
    'GB': CURRENCY_MULTIPLIERS['GBP'],
    'IN': CURRENCY_MULTIPLIERS['INR'],
    'PK': CURRENCY_MULTIPLIERS['PKR'],
    'CA': CURRENCY_MULTIPLIERS['CAD'],
    'AU': CURRENCY_MULTIPLIERS['AUD'],
    'DE': CURRENCY_MULTIPLIERS['EUR'],
    'FR': CURRENCY_MULTIPLIERS['EUR'],
}


def get_currency_multiplier(country_code: str) -> float:
    """
    Get currency multiplier for a given country code.
    Used to convert USD prices to local currency.
    
    Args:
        country_code: ISO 2-letter country code (e.g., 'US', 'GB', 'IN')
    
    Returns:
        Multiplier float (e.g., 1.0 for USD, 83.0 for INR)
    """
    if not country_code:
        return 1.0
    
    country_code = country_code.upper().strip()
    return COUNTRY_MULTIPLIERS.get(country_code, 1.0)


def convert_usd_to_local(usd_amount: float, country_code: str) -> float:
    """
    Convert USD amount to local currency for given country.
    
    Args:
        usd_amount: Amount in USD
        country_code: ISO 2-letter country code
    
    Returns:
        Amount in local currency
    """
    multiplier = get_currency_multiplier(country_code)
    return round(usd_amount * multiplier, 2)


def format_currency(amount: float, country_code: str = 'US') -> str:
    """
    Format amount with appropriate currency symbol.
    
    Args:
        amount: Numeric amount
        country_code: ISO 2-letter country code
    
    Returns:
        Formatted string (e.g., '$99.00', '₹8,300.00')
    """
    currency = get_currency_for_country(country_code)
    symbol = get_currency_symbol(currency)
    
    # Format with commas for thousands
    if amount >= 1000:
        formatted = f"{amount:,.2f}"
    else:
        formatted = f"{amount:.2f}"
    
    return f"{symbol}{formatted}"
