<?php

namespace PloverCRM\Modules\Integrations\Services;

/**
 * Currency Service
 * 
 * Provides ISO 4217 currency codes for conversion mappings.
 * Google Ads accepts any valid ISO 4217 3-character code.
 * 
 * @see https://developers.google.com/google-ads/api/data/codes-formats
 */
class CurrencyService
{
    /**
     * Get list of common ISO 4217 currencies
     * 
     * @return array Array of [code, name, symbol] arrays
     */
    public static function getCommonCurrencies(): array
    {
        return [
            ['code' => 'USD', 'name' => 'US Dollar', 'symbol' => '$'],
            ['code' => 'EUR', 'name' => 'Euro', 'symbol' => '€'],
            ['code' => 'GBP', 'name' => 'British Pound', 'symbol' => '£'],
            ['code' => 'JPY', 'name' => 'Japanese Yen', 'symbol' => '¥'],
            ['code' => 'AUD', 'name' => 'Australian Dollar', 'symbol' => 'A$'],
            ['code' => 'CAD', 'name' => 'Canadian Dollar', 'symbol' => 'C$'],
            ['code' => 'CHF', 'name' => 'Swiss Franc', 'symbol' => 'CHF'],
            ['code' => 'CNY', 'name' => 'Chinese Yuan', 'symbol' => '¥'],
            ['code' => 'INR', 'name' => 'Indian Rupee', 'symbol' => '₹'],
            ['code' => 'PKR', 'name' => 'Pakistani Rupee', 'symbol' => '₨'],
            ['code' => 'AED', 'name' => 'UAE Dirham', 'symbol' => 'د.إ'],
            ['code' => 'SAR', 'name' => 'Saudi Riyal', 'symbol' => '﷼'],
            ['code' => 'SGD', 'name' => 'Singapore Dollar', 'symbol' => 'S$'],
            ['code' => 'NZD', 'name' => 'New Zealand Dollar', 'symbol' => 'NZ$'],
            ['code' => 'MXN', 'name' => 'Mexican Peso', 'symbol' => '$'],
            ['code' => 'BRL', 'name' => 'Brazilian Real', 'symbol' => 'R$'],
            ['code' => 'ZAR', 'name' => 'South African Rand', 'symbol' => 'R'],
            ['code' => 'SEK', 'name' => 'Swedish Krona', 'symbol' => 'kr'],
            ['code' => 'NOK', 'name' => 'Norwegian Krone', 'symbol' => 'kr'],
            ['code' => 'DKK', 'name' => 'Danish Krone', 'symbol' => 'kr'],
            ['code' => 'PLN', 'name' => 'Polish Zloty', 'symbol' => 'zł'],
            ['code' => 'TRY', 'name' => 'Turkish Lira', 'symbol' => '₺'],
            ['code' => 'KRW', 'name' => 'South Korean Won', 'symbol' => '₩'],
            ['code' => 'RUB', 'name' => 'Russian Ruble', 'symbol' => '₽'],
            ['code' => 'THB', 'name' => 'Thai Baht', 'symbol' => '฿'],
            ['code' => 'MYR', 'name' => 'Malaysian Ringgit', 'symbol' => 'RM'],
            ['code' => 'IDR', 'name' => 'Indonesian Rupiah', 'symbol' => 'Rp'],
            ['code' => 'PHP', 'name' => 'Philippine Peso', 'symbol' => '₱'],
            ['code' => 'VND', 'name' => 'Vietnamese Dong', 'symbol' => '₫'],
            ['code' => 'EGP', 'name' => 'Egyptian Pound', 'symbol' => '£'],
            ['code' => 'NGN', 'name' => 'Nigerian Naira', 'symbol' => '₦'],
            ['code' => 'KES', 'name' => 'Kenyan Shilling', 'symbol' => 'KSh'],
            ['code' => 'GHS', 'name' => 'Ghanaian Cedi', 'symbol' => '₵'],
            ['code' => 'BDT', 'name' => 'Bangladeshi Taka', 'symbol' => '৳'],
            ['code' => 'LKR', 'name' => 'Sri Lankan Rupee', 'symbol' => '₨'],
            ['code' => 'QAR', 'name' => 'Qatari Riyal', 'symbol' => '﷼'],
            ['code' => 'KWD', 'name' => 'Kuwaiti Dinar', 'symbol' => 'د.ك'],
            ['code' => 'OMR', 'name' => 'Omani Rial', 'symbol' => '﷼'],
            ['code' => 'BHD', 'name' => 'Bahraini Dinar', 'symbol' => 'د.ب'],
            ['code' => 'JOD', 'name' => 'Jordanian Dinar', 'symbol' => 'د.ا'],
            ['code' => 'LBP', 'name' => 'Lebanese Pound', 'symbol' => 'ل.ل'],
            ['code' => 'ILS', 'name' => 'Israeli Shekel', 'symbol' => '₪'],
            ['code' => 'CLP', 'name' => 'Chilean Peso', 'symbol' => '$'],
            ['code' => 'COP', 'name' => 'Colombian Peso', 'symbol' => '$'],
            ['code' => 'ARS', 'name' => 'Argentine Peso', 'symbol' => '$'],
            ['code' => 'PEN', 'name' => 'Peruvian Sol', 'symbol' => 'S/'],
            ['code' => 'UYU', 'name' => 'Uruguayan Peso', 'symbol' => '$U'],
            ['code' => 'HKD', 'name' => 'Hong Kong Dollar', 'symbol' => 'HK$'],
            ['code' => 'TWD', 'name' => 'Taiwan Dollar', 'symbol' => 'NT$'],
            ['code' => 'CZK', 'name' => 'Czech Koruna', 'symbol' => 'Kč'],
            ['code' => 'HUF', 'name' => 'Hungarian Forint', 'symbol' => 'Ft'],
            ['code' => 'RON', 'name' => 'Romanian Leu', 'symbol' => 'lei'],
            ['code' => 'BGN', 'name' => 'Bulgarian Lev', 'symbol' => 'лв'],
            ['code' => 'HRK', 'name' => 'Croatian Kuna', 'symbol' => 'kn'],
            ['code' => 'RSD', 'name' => 'Serbian Dinar', 'symbol' => 'дин.'],
            ['code' => 'UAH', 'name' => 'Ukrainian Hryvnia', 'symbol' => '₴'],
            ['code' => 'MAD', 'name' => 'Moroccan Dirham', 'symbol' => 'د.م.'],
            ['code' => 'TND', 'name' => 'Tunisian Dinar', 'symbol' => 'د.ت'],
            ['code' => 'DZD', 'name' => 'Algerian Dinar', 'symbol' => 'د.ج'],
        ];
    }

    /**
     * Get all ISO 4217 currencies (comprehensive list)
     * 
     * @return array Array of [code, name] arrays
     */
    public static function getAllCurrencies(): array
    {
        // This can be expanded to include all 180+ currencies if needed
        // For now, return common currencies which covers 95% of use cases
        return self::getCommonCurrencies();
    }

    /**
     * Get currency by code
     * 
     * @param string $code ISO 4217 code
     * @return array|null Currency data or null
     */
    public static function getCurrency(string $code): ?array
    {
        $currencies = self::getCommonCurrencies();
        foreach ($currencies as $currency) {
            if ($currency['code'] === strtoupper($code)) {
                return $currency;
            }
        }
        return null;
    }

    /**
     * Check if a currency code is valid
     * 
     * @param string $code ISO 4217 code
     * @return bool
     */
    public static function isValidCurrency(string $code): bool
    {
        return self::getCurrency($code) !== null;
    }

    /**
     * Get currencies as options array for select dropdown
     * 
     * @return array [code => "Code - Name (Symbol)"]
     */
    public static function getCurrencyOptions(): array
    {
        $options = [];
        foreach (self::getCommonCurrencies() as $currency) {
            $options[$currency['code']] = sprintf(
                '%s - %s (%s)',
                $currency['code'],
                $currency['name'],
                $currency['symbol']
            );
        }
        return $options;
    }
}
