<?php

namespace App\Helpers;

class CountryHelper
{
    /**
     * Get country flag emoji based on country name
     * 
     * @param string $countryName
     * @return string|null
     */
    public static function getCountryFlag($countryName)
    {
        if (empty($countryName)) {
            return null;
        }

        // Mapping of common Arabic country names to flag emojis
        $countryFlags = [
            // Arabic countries
            'السعودية' => '🇸🇦',
            'المملكة العربية السعودية' => '🇸🇦',
            'مصر' => '🇪🇬',
            'الإمارات العربية المتحدة' => '🇦🇪',
            'الإمارات' => '🇦🇪',
            'الكويت' => '🇰🇼',
            'قطر' => '🇶🇦',
            'البحرين' => '🇧🇭',
            'عمان' => '🇴🇲',
            'اليمن' => '🇾🇪',
            'الأردن' => '🇯🇴',
            'لبنان' => '🇱🇧',
            'سوريا' => '🇸🇾',
            'العراق' => '🇮🇶',
            'فلسطين' => '🇵🇸',
            'السودان' => '🇸🇩',
            'ليبيا' => '🇱🇾',
            'تونس' => '🇹🇳',
            'الجزائر' => '🇩🇿',
            'المغرب' => '🇲🇦',
            'موريتانيا' => '🇲🇷',
            'جيبوتي' => '🇩🇯',
            'الصومال' => '🇸🇴',
            'جزر القمر' => '🇰🇲',
            
            // English country names
            'Saudi Arabia' => '🇸🇦',
            'Egypt' => '🇪🇬',
            'United Arab Emirates' => '🇦🇪',
            'UAE' => '🇦🇪',
            'Kuwait' => '🇰🇼',
            'Qatar' => '🇶🇦',
            'Bahrain' => '🇧🇭',
            'Oman' => '🇴🇲',
            'Yemen' => '🇾🇪',
            'Jordan' => '🇯🇴',
            'Lebanon' => '🇱🇧',
            'Syria' => '🇸🇾',
            'Iraq' => '🇮🇶',
            'Palestine' => '🇵🇸',
            'Sudan' => '🇸🇩',
            'Libya' => '🇱🇾',
            'Tunisia' => '🇹🇳',
            'Algeria' => '🇩🇿',
            'Morocco' => '🇲🇦',
            'Mauritania' => '🇲🇷',
            'Djibouti' => '🇩🇯',
            'Somalia' => '🇸🇴',
            'Comoros' => '🇰🇲',
            
            // Other common countries
            'United States' => '🇺🇸',
            'USA' => '🇺🇸',
            'United Kingdom' => '🇬🇧',
            'UK' => '🇬🇧',
            'Canada' => '🇨🇦',
            'Australia' => '🇦🇺',
            'Germany' => '🇩🇪',
            'France' => '🇫🇷',
            'Italy' => '🇮🇹',
            'Spain' => '🇪🇸',
            'Turkey' => '🇹🇷',
            'India' => '🇮🇳',
            'China' => '🇨🇳',
            'Japan' => '🇯🇵',
            'South Korea' => '🇰🇷',
            'Brazil' => '🇧🇷',
            'Russia' => '🇷🇺',
            'South Africa' => '🇿🇦',
            'Nigeria' => '🇳🇬',
            'Kenya' => '🇰🇪',
            'Malaysia' => '🇲🇾',
            'Indonesia' => '🇮🇩',
            'Pakistan' => '🇵🇰',
            'Bangladesh' => '🇧🇩',
            'Philippines' => '🇵🇭',
            'Thailand' => '🇹🇭',
            'Vietnam' => '🇻🇳',
            'Singapore' => '🇸🇬',
            'New Zealand' => '🇳🇿',
            'Sweden' => '🇸🇪',
            'Norway' => '🇳🇴',
            'Denmark' => '🇩🇰',
            'Finland' => '🇫🇮',
            'Netherlands' => '🇳🇱',
            'Belgium' => '🇧🇪',
            'Switzerland' => '🇨🇭',
            'Austria' => '🇦🇹',
            'Poland' => '🇵🇱',
            'Greece' => '🇬🇷',
            'Portugal' => '🇵🇹',
            'Ireland' => '🇮🇪',
            'Israel' => '🇮🇱',
            'Iran' => '🇮🇷',
            'Afghanistan' => '🇦🇫',
        ];

        // Normalize country name (remove extra spaces, convert to lowercase for comparison)
        $normalizedName = trim($countryName);
        
        // Check exact match first
        if (isset($countryFlags[$normalizedName])) {
            return $countryFlags[$normalizedName];
        }

        // Check case-insensitive match
        foreach ($countryFlags as $key => $flag) {
            if (mb_strtolower($normalizedName) === mb_strtolower($key)) {
                return $flag;
            }
        }

        // Check if country name contains any of the keys
        foreach ($countryFlags as $key => $flag) {
            if (stripos($normalizedName, $key) !== false || stripos($key, $normalizedName) !== false) {
                return $flag;
            }
        }

        // Default: return a generic flag or null
        return null;
    }
}










