package utils

import (
	"regexp"
	"slices"
	"strings"

	"github.com/juju/errors"
)

type AzureRegion struct {
	FullName    string
	Name        string
	DisplayName string
	Country     string
	TimeZone    string
}

var AZURE_AREA_PATTERN = regexp.MustCompile(`\([^\)]+\)`)

var azureRegions []AzureRegion = []AzureRegion{
	{FullName: "(Africa) South Africa North", DisplayName: "South Africa North", Name: "southafricanorth", Country: "AF", TimeZone: "UTC+2"},
	{FullName: "(Africa) South Africa West", DisplayName: "South Africa West", Name: "southafricawest", Country: "AF", TimeZone: "UTC+2"},
	{FullName: "(Asia Pacific) Australia Central 2", DisplayName: "Australia Central 2", Name: "australiacentral2", Country: "AU", TimeZone: "UTC+11"},
	{FullName: "(Asia Pacific) Australia Central", DisplayName: "Australia Central", Name: "australiacentral", Country: "AU", TimeZone: "UTC+11"},
	{FullName: "(Asia Pacific) Australia East", DisplayName: "Australia East", Name: "australiaeast", Country: "AU", TimeZone: "UTC+11"},
	{FullName: "(Asia Pacific) Australia Southeast", DisplayName: "Australia Southeast", Name: "australiasoutheast", Country: "AU", TimeZone: "UTC+11"},
	{FullName: "(Asia Pacific) Central India", DisplayName: "Central India", Name: "centralindia", Country: "IN", TimeZone: "UTC+5:30"},
	{FullName: "(Asia Pacific) East Asia", DisplayName: "East Asia", Name: "eastasia", Country: "CN", TimeZone: "UTC+8"},
	{FullName: "(Asia Pacific) Japan East", DisplayName: "Japan East", Name: "japaneast", Country: "JP", TimeZone: "UTC+9"},
	{FullName: "(Asia Pacific) Japan West", DisplayName: "Japan West", Name: "japanwest", Country: "JP", TimeZone: "UTC+9"},
	{FullName: "(Asia Pacific) Jio India Central", DisplayName: "Jio India Central", Name: "jioindiacentral", Country: "IN", TimeZone: "UTC+5:30"},
	{FullName: "(Asia Pacific) Jio India West", DisplayName: "Jio India West", Name: "jioindiawest", Country: "IN", TimeZone: "UTC+5:30"},
	{FullName: "(Asia Pacific) Korea Central", DisplayName: "Korea Central", Name: "koreacentral", Country: "KR", TimeZone: "UTC+9"},
	{FullName: "(Asia Pacific) Korea South", DisplayName: "Korea South", Name: "koreasouth", Country: "KR", TimeZone: "UTC+9"},
	{FullName: "(Asia Pacific) New Zealand North", DisplayName: "New Zealand North", Name: "newzealandnorth", Country: "NZ", TimeZone: "UTC+13"},
	{FullName: "(Asia Pacific) South India", DisplayName: "South India", Name: "southindia", Country: "IN", TimeZone: "UTC+5:30"},
	{FullName: "(Asia Pacific) Southeast Asia", DisplayName: "Southeast Asia", Name: "southeastasia", Country: "SG", TimeZone: "UTC+8"},
	{FullName: "(Asia Pacific) West India", DisplayName: "West India", Name: "westindia", Country: "IN", TimeZone: "UTC+5:30"},
	{FullName: "(Canada) Canada Central", DisplayName: "Canada Central", Name: "canadacentral", Country: "CA", TimeZone: "UTC-5"},
	{FullName: "(Canada) Canada East", DisplayName: "Canada East", Name: "canadaeast", Country: "CA", TimeZone: "UTC-5"},
	{FullName: "(Europe) France Central", DisplayName: "France Central", Name: "francecentral", Country: "FR", TimeZone: "UTC+1"},
	{FullName: "(Europe) France South", DisplayName: "France South", Name: "francesouth", Country: "FR", TimeZone: "UTC+1"},
	{FullName: "(Europe) Germany North", DisplayName: "Germany North", Name: "germanynorth", Country: "DE", TimeZone: "UTC+1"},
	{FullName: "(Europe) Germany West Central", DisplayName: "Germany West Central", Name: "germanywestcentral", Country: "DE", TimeZone: "UTC+1"},
	{FullName: "(Europe) Italy North", DisplayName: "Italy North", Name: "italynorth", Country: "IT", TimeZone: "UTC+1"},
	{FullName: "(Europe) North Europe", DisplayName: "North Europe", Name: "northeurope", Country: "IE", TimeZone: "UTC+0"},
	{FullName: "(Europe) Norway East", DisplayName: "Norway East", Name: "norwayeast", Country: "NO", TimeZone: "UTC+1"},
	{FullName: "(Europe) Norway West", DisplayName: "Norway West", Name: "norwaywest", Country: "NO", TimeZone: "UTC+1"},
	{FullName: "(Europe) Poland Central", DisplayName: "Poland Central", Name: "polandcentral", Country: "PL", TimeZone: "UTC+1"},
	{FullName: "(Europe) Spain Central", DisplayName: "Spain Central", Name: "spaincentral", Country: "ES", TimeZone: "UTC+1"},
	{FullName: "(Europe) Sweden Central", DisplayName: "Sweden Central", Name: "swedencentral", Country: "SE", TimeZone: "UTC+1"},
	{FullName: "(Europe) Switzerland North", DisplayName: "Switzerland North", Name: "switzerlandnorth", Country: "CH", TimeZone: "UTC+1"},
	{FullName: "(Europe) Switzerland West", DisplayName: "Switzerland West", Name: "switzerlandwest", Country: "CH", TimeZone: "UTC+1"},
	{FullName: "(Europe) UK South", DisplayName: "UK South", Name: "uksouth", Country: "UK", TimeZone: "UTC+0"},
	{FullName: "(Europe) UK West", DisplayName: "UK West", Name: "ukwest", Country: "UK", TimeZone: "UTC+0"},
	{FullName: "(Europe) West Europe", DisplayName: "West Europe", Name: "westeurope", Country: "NL", TimeZone: "UTC+1"},
	{FullName: "(Mexico) Mexico Central", DisplayName: "Mexico Central", Name: "mexicocentral", Country: "MX", TimeZone: "UTC-6"},
	{FullName: "(Middle East) Israel Central", DisplayName: "Israel Central", Name: "israelcentral", Country: "IL", TimeZone: "UTC+2"},
	{FullName: "(Middle East) Qatar Central", DisplayName: "Qatar Central", Name: "qatarcentral", Country: "QA", TimeZone: "UTC+3"},
	{FullName: "(Middle East) UAE Central", DisplayName: "UAE Central", Name: "uaecentral", Country: "AE", TimeZone: "UTC+4"},
	{FullName: "(Middle East) UAE North", DisplayName: "UAE North", Name: "uaenorth", Country: "AE", TimeZone: "UTC+4"},
	{FullName: "(South America) Brazil South", DisplayName: "Brazil South", Name: "brazilsouth", Country: "BR", TimeZone: "UTC-3"},
	{FullName: "(South America) Brazil Southeast", DisplayName: "Brazil Southeast", Name: "brazilsoutheast", Country: "BR", TimeZone: "UTC-3"},
	{FullName: "(South America) Brazil US", DisplayName: "Brazil US", Name: "brazilus", Country: "BR", TimeZone: "UTC-3"},
	{FullName: "(US) Central US EUAP", DisplayName: "Central US EUAP", Name: "centraluseuap", Country: "US", TimeZone: "UTC-6"},
	{FullName: "(US) Central US", DisplayName: "Central US", Name: "centralus", Country: "US", TimeZone: "UTC-6"},
	{FullName: "(US) East US 2 EUAP", DisplayName: "East US 2 EUAP", Name: "eastus2euap", Country: "US", TimeZone: "UTC-6"},
	{FullName: "(US) East US 2", DisplayName: "East US 2", Name: "eastus2", Country: "US", TimeZone: "UTC-5"},
	{FullName: "(US) East US STG", DisplayName: "East US STG", Name: "eastusstg", Country: "US", TimeZone: "UTC-5"},
	{FullName: "(US) East US", DisplayName: "East US", Name: "eastus", Country: "US", TimeZone: "UTC-5"},
	{FullName: "(US) North Central US", DisplayName: "North Central US", Name: "northcentralus", Country: "US", TimeZone: "UTC-6"},
	{FullName: "(US) South Central US STG", DisplayName: "South Central US STG", Name: "southcentralusstg", Country: "US", TimeZone: "UTC-6"},
	{FullName: "(US) South Central US", DisplayName: "South Central US", Name: "southcentralus", Country: "US", TimeZone: "UTC-6"},
	{FullName: "(US) West Central US", DisplayName: "West Central US", Name: "westcentralus", Country: "US", TimeZone: "UTC-7"},
	{FullName: "(US) West US 2", DisplayName: "West US 2", Name: "westus2", Country: "US", TimeZone: "UTC-8"},
	{FullName: "(US) West US 3", DisplayName: "West US 3", Name: "westus3", Country: "US", TimeZone: "UTC-7"},
	{FullName: "(US) West US", DisplayName: "West US", Name: "westus", Country: "US", TimeZone: "UTC-8"},
}

func GetAzureRegionName(region string) (string, error) {
	index := slices.IndexFunc(azureRegions, func(e AzureRegion) bool {
		return e.Name == region
	})
	if index < 0 {
		return "", errors.NotFoundf("Region %s", region)
	}

	return azureRegions[index].FullName, nil
}

func GetAzureRegionArea(region string) (string, error) {
	index := slices.IndexFunc(azureRegions, func(e AzureRegion) bool {
		return e.Name == region
	})
	if index < 0 {
		return "", errors.NotFoundf("Region %s", region)
	}

	area := string(AZURE_AREA_PATTERN.Find([]byte(azureRegions[index].FullName)))
	return strings.Trim(area, "()"), nil
}

func GetAzureRegionCountryCode(region string) (string, error) {
	index := slices.IndexFunc(azureRegions, func(e AzureRegion) bool {
		return e.Name == region

	})
	if index < 0 {
		return "", errors.NotFoundf("Region %s", region)
	}

	return azureRegions[index].Country, nil
}

func GetAzureRegionBackupTime(region string) (string, error) {
	index := slices.IndexFunc(azureRegions, func(e AzureRegion) bool {
		return e.Name == region

	})
	if index < 0 {
		return "", errors.NotFoundf("Region %s", region)
	}
	return GetBackupTimeByTimeZone(azureRegions[index].TimeZone)
}
