package assets

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

// CountryCodeMapping maps 3-letter country codes to 2-letter ISO codes
var CountryCodeMapping = map[string]string{
	"POL": "pl", // Poland
	"GRC": "gr", // Greece
	"DEU": "de", // Germany
	"FRA": "fr", // France
	"ITA": "it", // Italy
	"ESP": "es", // Spain
	"GBR": "gb", // United Kingdom
	"NLD": "nl", // Netherlands
	"BEL": "be", // Belgium
	"AUT": "at", // Austria
	"SWE": "se", // Sweden
	"DNK": "dk", // Denmark
	"FIN": "fi", // Finland
	"NOR": "no", // Norway
	"IRL": "ie", // Ireland
	"PRT": "pt", // Portugal
	"CZE": "cz", // Czech Republic
	"SVK": "sk", // Slovakia
	"HUN": "hu", // Hungary
	"ROU": "ro", // Romania
	"BGR": "bg", // Bulgaria
	"HRV": "hr", // Croatia
	"SVN": "si", // Slovenia
	"EST": "ee", // Estonia
	"LVA": "lv", // Latvia
	"LTU": "lt", // Lithuania
	"LUX": "lu", // Luxembourg
	"MLT": "mt", // Malta
	"CYP": "cy", // Cyprus
	"USA": "us", // United States
	"CAN": "ca", // Canada
	"AUS": "au", // Australia
	"JPN": "jp", // Japan
	"CHN": "cn", // China
	"IND": "in", // India
	"BRA": "br", // Brazil
	"RUS": "ru", // Russia
	"TUR": "tr", // Turkey
	"UKR": "ua", // Ukraine
	"EGY": "eg", // Egypt
	"ZAF": "za", // South Africa
	"NZL": "nz", // New Zealand
	"KOR": "kr", // South Korea
	"THA": "th", // Thailand
	"SGP": "sg", // Singapore
	"MYS": "my", // Malaysia
	"IDN": "id", // Indonesia
	"PHL": "ph", // Philippines
	"VNM": "vn", // Vietnam
	"CHL": "cl", // Chile
	"ARG": "ar", // Argentina
	"MEX": "mx", // Mexico
	"PER": "pe", // Peru
	"COL": "co", // Colombia
	"VEN": "ve", // Venezuela
	"ECU": "ec", // Ecuador
	"BOL": "bo", // Bolivia
	"URY": "uy", // Uruguay
	"PRY": "py", // Paraguay
	"GUY": "gy", // Guyana
	"SUR": "sr", // Suriname
	"ISR": "il", // Israel
	"SAU": "sa", // Saudi Arabia
	"ARE": "ae", // United Arab Emirates
	"QAT": "qa", // Qatar
	"KWT": "kw", // Kuwait
	"BHR": "bh", // Bahrain
	"OMN": "om", // Oman
	"JOR": "jo", // Jordan
	"LBN": "lb", // Lebanon
	"SYR": "sy", // Syria
	"IRQ": "iq", // Iraq
	"IRN": "ir", // Iran
	"AFG": "af", // Afghanistan
	"PAK": "pk", // Pakistan
	"BGD": "bd", // Bangladesh
	"LKA": "lk", // Sri Lanka
	"MDV": "mv", // Maldives
	"BTN": "bt", // Bhutan
	"NPL": "np", // Nepal
	"MMR": "mm", // Myanmar
	"LAO": "la", // Laos
	"KHM": "kh", // Cambodia
	"BRN": "bn", // Brunei
	"TLS": "tl", // East Timor
	"PNG": "pg", // Papua New Guinea
	"FJI": "fj", // Fiji
	"TON": "to", // Tonga
	"WSM": "ws", // Samoa
	"VUT": "vu", // Vanuatu
	"SLB": "sb", // Solomon Islands
	"KIR": "ki", // Kiribati
	"TUV": "tv", // Tuvalu
	"NRU": "nr", // Nauru
	"PLW": "pw", // Palau
	"FSM": "fm", // Federated States of Micronesia
	"MHL": "mh", // Marshall Islands
	"COK": "ck", // Cook Islands
	"NIU": "nu", // Niue
	"TKL": "tk", // Tokelau
	"WLF": "wf", // Wallis and Futuna
	"ASM": "as", // American Samoa
	"GUM": "gu", // Guam
	"MNP": "mp", // Northern Mariana Islands
	"PRI": "pr", // Puerto Rico
	"VIR": "vi", // U.S. Virgin Islands
	"BHS": "bs", // Bahamas
	"BLZ": "bz", // Belize
	"CRI": "cr", // Costa Rica
	"GTM": "gt", // Guatemala
	"HND": "hn", // Honduras
	"NIC": "ni", // Nicaragua
	"PAN": "pa", // Panama
	"SLV": "sv", // El Salvador
	"JAM": "jm", // Jamaica
	"HTI": "ht", // Haiti
	"DOM": "do", // Dominican Republic
	"CUB": "cu", // Cuba
	"TTO": "tt", // Trinidad and Tobago
	"BRB": "bb", // Barbados
	"LCA": "lc", // Saint Lucia
	"VCT": "vc", // Saint Vincent and the Grenadines
	"GRD": "gd", // Grenada
	"ATG": "ag", // Antigua and Barbuda
	"DMA": "dm", // Dominica
	"KNA": "kn", // Saint Kitts and Nevis
	"BWA": "bw", // Botswana
	"ZWE": "zw", // Zimbabwe
	"ZMB": "zm", // Zambia
	"MWI": "mw", // Malawi
	"MOZ": "mz", // Mozambique
	"MDG": "mg", // Madagascar
	"MUS": "mu", // Mauritius
	"SYC": "sc", // Seychelles
	"COM": "km", // Comoros
	"DJI": "dj", // Djibouti
	"ERI": "er", // Eritrea
	"ETH": "et", // Ethiopia
	"KEN": "ke", // Kenya
	"TZA": "tz", // Tanzania
	"UGA": "ug", // Uganda
	"RWA": "rw", // Rwanda
	"BDI": "bi", // Burundi
	"SSD": "ss", // South Sudan
	"SDN": "sd", // Sudan
	"LBY": "ly", // Libya
	"TUN": "tn", // Tunisia
	"DZA": "dz", // Algeria
	"MAR": "ma", // Morocco
	"ESH": "eh", // Western Sahara
	"MRT": "mr", // Mauritania
	"MLI": "ml", // Mali
	"BFA": "bf", // Burkina Faso
	"NER": "ne", // Niger
	"TCD": "td", // Chad
	"CAF": "cf", // Central African Republic
	"CMR": "cm", // Cameroon
	"GAB": "ga", // Gabon
	"GNQ": "gq", // Equatorial Guinea
	"STP": "st", // São Tomé and Príncipe
	"GNB": "gw", // Guinea-Bissau
	"GIN": "gn", // Guinea
	"SLE": "sl", // Sierra Leone
	"LBR": "lr", // Liberia
	"CIV": "ci", // Ivory Coast
	"GHA": "gh", // Ghana
	"TGO": "tg", // Togo
	"BEN": "bj", // Benin
	"NGA": "ng", // Nigeria
	"COG": "cg", // Republic of the Congo
	"COD": "cd", // Democratic Republic of the Congo
	"AGO": "ao", // Angola
	"NAM": "na", // Namibia
	"LSO": "ls", // Lesotho
	"SWZ": "sz", // Eswatini
}

// GetISOCode returns the 2-letter ISO code for a 3-letter country code
func GetISOCode(countryCode string) string {
	if isoCode, exists := CountryCodeMapping[strings.ToUpper(countryCode)]; exists {
		return isoCode
	}
	return "" // Return empty string if not found
}

// IsValidCountryCode checks if a 3-letter country code is valid
func IsValidCountryCode(countryCode string) bool {
	_, exists := CountryCodeMapping[strings.ToUpper(countryCode)]
	return exists
}

// GetCountryName returns the full country name for a 3-letter country code
func GetCountryName(countryCode string) string {
	// This is a simplified mapping - in a real application, you might want to use a more comprehensive mapping
	countryNames := map[string]string{
		"POL": "Poland",
		"GRC": "Greece",
		"DEU": "Germany",
		"FRA": "France",
		"ITA": "Italy",
		"ESP": "Spain",
		"GBR": "United Kingdom",
		"NLD": "Netherlands",
		"BEL": "Belgium",
		"AUT": "Austria",
		"SWE": "Sweden",
		"DNK": "Denmark",
		"FIN": "Finland",
		"NOR": "Norway",
		"IRL": "Ireland",
		"PRT": "Portugal",
		"CZE": "Czech Republic",
		"SVK": "Slovakia",
		"HUN": "Hungary",
		"ROU": "Romania",
		"BGR": "Bulgaria",
		"HRV": "Croatia",
		"SVN": "Slovenia",
		"EST": "Estonia",
		"LVA": "Latvia",
		"LTU": "Lithuania",
		"LUX": "Luxembourg",
		"MLT": "Malta",
		"CYP": "Cyprus",
		"USA": "United States",
		"CAN": "Canada",
		"AUS": "Australia",
		"JPN": "Japan",
		"CHN": "China",
		"IND": "India",
		"BRA": "Brazil",
		"RUS": "Russia",
		"TUR": "Turkey",
		"UKR": "Ukraine",
		"EGY": "Egypt",
		"ZAF": "South Africa",
		"NZL": "New Zealand",
		"KOR": "South Korea",
		"THA": "Thailand",
		"SGP": "Singapore",
		"MYS": "Malaysia",
		"IDN": "Indonesia",
		"PHL": "Philippines",
		"VNM": "Vietnam",
		"CHL": "Chile",
		"ARG": "Argentina",
		"MEX": "Mexico",
		"PER": "Peru",
		"COL": "Colombia",
		"VEN": "Venezuela",
		"ECU": "Ecuador",
		"BOL": "Bolivia",
		"URY": "Uruguay",
		"PRY": "Paraguay",
		"GUY": "Guyana",
		"SUR": "Suriname",
		"ISR": "Israel",
		"SAU": "Saudi Arabia",
		"ARE": "United Arab Emirates",
		"QAT": "Qatar",
		"KWT": "Kuwait",
		"BHR": "Bahrain",
		"OMN": "Oman",
		"JOR": "Jordan",
		"LBN": "Lebanon",
		"SYR": "Syria",
		"IRQ": "Iraq",
		"IRN": "Iran",
		"AFG": "Afghanistan",
		"PAK": "Pakistan",
		"BGD": "Bangladesh",
		"LKA": "Sri Lanka",
		"MDV": "Maldives",
		"BTN": "Bhutan",
		"NPL": "Nepal",
		"MMR": "Myanmar",
		"LAO": "Laos",
		"KHM": "Cambodia",
		"BRN": "Brunei",
		"TLS": "East Timor",
		"PNG": "Papua New Guinea",
		"FJI": "Fiji",
		"TON": "Tonga",
		"WSM": "Samoa",
		"VUT": "Vanuatu",
		"SLB": "Solomon Islands",
		"KIR": "Kiribati",
		"TUV": "Tuvalu",
		"NRU": "Nauru",
		"PLW": "Palau",
		"FSM": "Federated States of Micronesia",
		"MHL": "Marshall Islands",
		"COK": "Cook Islands",
		"NIU": "Niue",
		"TKL": "Tokelau",
		"WLF": "Wallis and Futuna",
		"ASM": "American Samoa",
		"GUM": "Guam",
		"MNP": "Northern Mariana Islands",
		"PRI": "Puerto Rico",
		"VIR": "U.S. Virgin Islands",
		"BHS": "Bahamas",
		"BLZ": "Belize",
		"CRI": "Costa Rica",
		"GTM": "Guatemala",
		"HND": "Honduras",
		"NIC": "Nicaragua",
		"PAN": "Panama",
		"SLV": "El Salvador",
		"JAM": "Jamaica",
		"HTI": "Haiti",
		"DOM": "Dominican Republic",
		"CUB": "Cuba",
		"TTO": "Trinidad and Tobago",
		"BRB": "Barbados",
		"LCA": "Saint Lucia",
		"VCT": "Saint Vincent and the Grenadines",
		"GRD": "Grenada",
		"ATG": "Antigua and Barbuda",
		"DMA": "Dominica",
		"KNA": "Saint Kitts and Nevis",
		"BWA": "Botswana",
		"ZWE": "Zimbabwe",
		"ZMB": "Zambia",
		"MWI": "Malawi",
		"MOZ": "Mozambique",
		"MDG": "Madagascar",
		"MUS": "Mauritius",
		"SYC": "Seychelles",
		"COM": "Comoros",
		"DJI": "Djibouti",
		"ERI": "Eritrea",
		"ETH": "Ethiopia",
		"KEN": "Kenya",
		"TZA": "Tanzania",
		"UGA": "Uganda",
		"RWA": "Rwanda",
		"BDI": "Burundi",
		"SSD": "South Sudan",
		"SDN": "Sudan",
		"LBY": "Libya",
		"TUN": "Tunisia",
		"DZA": "Algeria",
		"MAR": "Morocco",
		"ESH": "Western Sahara",
		"MRT": "Mauritania",
		"MLI": "Mali",
		"BFA": "Burkina Faso",
		"NER": "Niger",
		"TCD": "Chad",
		"CAF": "Central African Republic",
		"CMR": "Cameroon",
		"GAB": "Gabon",
		"GNQ": "Equatorial Guinea",
		"STP": "São Tomé and Príncipe",
		"GNB": "Guinea-Bissau",
		"GIN": "Guinea",
		"SLE": "Sierra Leone",
		"LBR": "Liberia",
		"CIV": "Ivory Coast",
		"GHA": "Ghana",
		"TGO": "Togo",
		"BEN": "Benin",
		"NGA": "Nigeria",
		"COG": "Republic of the Congo",
		"COD": "Democratic Republic of the Congo",
		"AGO": "Angola",
		"NAM": "Namibia",
		"LSO": "Lesotho",
		"SWZ": "Eswatini",
	}

	if name, exists := countryNames[strings.ToUpper(countryCode)]; exists {
		return name
	}
	return countryCode // Return the code itself if name not found
}

// GetFlagAsset returns the flag SVG asset from filesystem
func GetFlagAsset(flagsPath, isoCode string) ([]byte, error) {
	flagPath := filepath.Join(flagsPath, strings.ToLower(isoCode)+".svg")

	f, err := os.ReadFile(flagPath)
	if err != nil {
		return nil, fmt.Errorf("failed to read flag asset, %v", flagPath)
	}

	return f, nil
}
