view: awal_facts_prod_dim_country {
  sql_table_name: facts.prod.dim_country ;;

  dimension: country_id {
    type: number
    sql: ${TABLE}.countryid ;;
  }

  dimension: country_name {
    label: "
    {% if _view._name == 'label_country' %} Label Country
    {% elsif _view._name == 'subaccount_country' %} Subaccount Country
    {% elsif _view._name == 'artist_country' %} Artist Country
    {% elsif _view._name == 'market_country' %} Market Country
    {% elsif _view._name == 'tax_id_country' %} Tax ID Country
    {% elsif _view._name == 'product_territory_split_country' %} Product Territory Split Country
    {% else %} Country Name {% endif %}"
    type: string
    sql: ${TABLE}.countryname ;;
  }

  dimension: country_code {
    type: string
    map_layer_name: countries
    sql: ${TABLE}.country_code ;;
  }

  dimension: continent_id {
    type: number
    sql: ${TABLE}.continent_id ;;
  }

  dimension: continent_name {
    type: string
    sql: ${TABLE}.continent ;;
    label: "
    {% if _view._name == 'label_country' %} Label Continent
    {% else %} Continent {% endif %}"
  }

# Country Groups
  dimension: us_vs_row {
    label: "Group By: USA vs. Rest of World"
    case: {
      when: { sql: ${TABLE}.countryid = 1 ;; label: "USA" }
      else: "Rest of World"
    }
  }

  dimension: territory_leads {
    label: "Group By: Territory Leads"
    description: "Previously labelled 'Country Code - Territory Leads Grouping' in deprecated Market Share model"
    case: {
      when: { sql: ${TABLE}.country_code in ('US','CA') ;; label: "North America ex. Mexico" }
      when: { sql: ${TABLE}.country_code in ('BR') ;; label: "Brazil" }
      when: { sql: ${TABLE}.country_code in ('MX') ;; label: "Mexico" }
      when: { sql: ${TABLE}.country_code in ('TR') ;; label: "Turkey" }
      when: { sql: ${TABLE}.country_code in ('ZA') ;; label: "South Africa" }
      when: { sql: ${TABLE}.country_code in ('AU','NZ') ;; label: "Australia, New Zealand" }
      when: { sql: ${TABLE}.country_code in ('DE','AT','CH') ;; label: "Austria, Germany, Switzerland" }
      when: { sql: ${TABLE}.country_code in ('FR','LU','NL','BE') ;; label: "BeNeLux" }
      when: { sql: ${TABLE}.country_code in ('FI','SE','DK','NO') ;; label: "Nordics" }
      when: { sql: ${TABLE}.country_code in ('ES','IT','GR','PT') ;; label: "Spain, Italy, Portugal, Greece" }
      when: { sql: ${TABLE}.country_code in ('PH','MY','ID','TW','CN','JP','KR') ;; label: "Asia" }
      when: { sql: ${TABLE}.country_code in ('BG', 'CY', 'CZ', 'EE', 'GR', 'HU', 'LV', 'LT', 'MT', 'PL', 'RO', 'SK', 'SI') ;; label: "Eastern Europe" }
      when: { sql: ${TABLE}.country_code in ('GB', 'IE') ;; label: "United Kingdom" }
      when: { sql: ${TABLE}.country_code in ('AR', 'CL', 'CO', 'BZ', 'BO', 'CR', 'DM', 'DO', 'EC', 'SV', 'GT', 'HN', 'NI', 'PA', 'PY', 'PE', 'VE') ;; label: "LatAm" }
      else: "Rest of World"
    }
  }

  dimension: management {
    label: "Group By: Management"
    description: "Previously labelled 'Country Code - MGMT Grouping' in deprecated Market Share model"
    case: {
      when: { sql: ${TABLE}.countryid = 1 ;; label: "USA" }
      when: { sql: ${TABLE}.countryid = 2 ;; label: "Canada" }
      when: { sql: ${TABLE}.countryid = 3 ;; label: "United Kingdom" }
      when: { sql: ${TABLE}.countryid = 4 ;; label: "Germany" }
      when: { sql: ${TABLE}.countryid = 5 ;; label: "Sweden" }
      when: { sql: ${TABLE}.countryid = 7 ;; label: "Mexico" }
      when: { sql: ${TABLE}.countryid = 13 ;; label: "Brazil" }
      when: { sql: ${TABLE}.countryid = 15 ;; label: "Spain" }
      when: { sql: ${TABLE}.countryid = 23 ;; label: "Argentina" }
      when: { sql: ${TABLE}.countryid = 141 ;; label: "Philippines" }
      else: "Rest of World"
    }
  }

  dimension: core_markets {
    label: "Group By: Core Markets"
    description: "Previously labelled 'Coremarkets' in deprecated Market Share model"
    case: {
      when: { sql: ${TABLE}.countryid = 1 ;; label: "USA" }
      when: { sql: ${TABLE}.countryid = 2 ;; label: "Canada" }
      when: { sql: ${TABLE}.countryid = 3 ;; label: "United Kingdom" }
      when: { sql: ${TABLE}.countryid = 4 ;; label: "Germany" }
      when: { sql: ${TABLE}.countryid = 5 ;; label: "Sweden" }
      when: { sql: ${TABLE}.countryid = 6 ;; label: "France" }
      when: { sql: ${TABLE}.countryid = 7 ;; label: "Mexico" }
      when: { sql: ${TABLE}.countryid = 8 ;; label: "Australia" }
      when: { sql: ${TABLE}.countryid = 9 ;; label: "Japan" }
      when: { sql: ${TABLE}.countryid = 11 ;; label: "Switzerland" }
      when: { sql: ${TABLE}.countryid = 12 ;; label: "Italy" }
      when: { sql: ${TABLE}.countryid = 13 ;; label: "Brazil" }
      when: { sql: ${TABLE}.countryid = 14 ;; label: "Greece" }
      when: { sql: ${TABLE}.countryid = 15 ;; label: "Spain" }
      when: { sql: ${TABLE}.countryid = 16 ;; label: "Austria" }
      when: { sql: ${TABLE}.countryid = 23 ;; label: "Argentina" }
      when: { sql: ${TABLE}.countryid = 31 ;; label: "Belgium" }
      when: { sql: ${TABLE}.countryid = 48 ;; label: "Chile" }
      when: { sql: ${TABLE}.countryid = 50 ;; label: "Colombia" }
      when: { sql: ${TABLE}.countryid = 58 ;; label: "Czech Republic" }
      when: { sql: ${TABLE}.countryid = 59 ;; label: "Denmark" }
      when: { sql: ${TABLE}.countryid = 71 ;; label: "Finland" }
      when: { sql: ${TABLE}.countryid = 86 ;; label: "India" }
      when: { sql: ${TABLE}.countryid = 90 ;; label: "Ireland" }
      when: { sql: ${TABLE}.countryid = 108 ;; label: "Luxembourg" }
      when: { sql: ${TABLE}.countryid = 128 ;; label: "Netherlands" }
      when: { sql: ${TABLE}.countryid = 129 ;; label: "New Zealand" }
      when: { sql: ${TABLE}.countryid = 133 ;; label: "Norway" }
      when: { sql: ${TABLE}.countryid = 142 ;; label: "Poland" }
      when: { sql: ${TABLE}.countryid = 143 ;; label: "Portugal" }
      when: { sql: ${TABLE}.countryid = 146 ;; label: "Russia" }
      when: { sql: ${TABLE}.countryid = 163 ;; label: "South Africa" }
      when: { sql: ${TABLE}.countryid = 171 ;; label: "Thailand" }
      when: { sql: ${TABLE}.countryid = 176 ;; label: "Turkey" }
      when: { sql: ${TABLE}.countryid = 191 ;; label: "Taiwan, Province Of China" }
      when: { sql: ${TABLE}.countryid = 215 ;; label: "Hong Kong" }
      else: "Rest of World"
    }
  }

  dimension: region_groups {
    label: "Group By: Region Groups"
    description: "Previously labelled 'Region Group' in deprecated Market Share model"
    case: {
      when: { sql: ${TABLE}.country_code in ('US','CA') ;; label: "North America" }
      when: { sql: ${TABLE}.country_code in ('FI','SE','DK','NO','IS') ;; label: "Nordics" }
      when: { sql: ${TABLE}.country_code in ('DE','AT','CH') ;; label: "GSA" }
      when: { sql: ${TABLE}.country_code in ('LU','NL','BE') ;; label: "BeNeLux" }
      when: { sql: ${TABLE}.country_code in ('GB', 'IE') ;; label: "United Kingdom, Ireland" }
      when: { sql: ${TABLE}.country_code in ('FR') ;; label: "France" }
      when: { sql: ${TABLE}.country_code in ('ES','PT') ;; label: "Spain, Portugal" }
      when: { sql: ${TABLE}.country_code in ('IT','GR') ;; label: "Italy, Greece" }
      when: { sql: ${TABLE}.country_code in ('PL', 'CZ', 'HU', 'SK', 'LV')  ;; label: "EE Core" }
      when: { sql: ${TABLE}.country_code in ('RO', 'BG', 'CS', 'HR', 'UA', 'LT', 'EE')  ;; label: "EE Core II" }
      when: { sql: ${TABLE}.country_code in ('TR') ;; label: "Turkey" }
      when: { sql: ${TABLE}.country_code in ('AR', 'BR', 'MX', 'UY', 'CO', 'CL', 'PE') ;; label: "LatAm Core" }
      when: { sql: ${TABLE}.country_code in ('AU', 'NZ') ;; label: "Australia, New Zealand" }
      when: { sql: ${TABLE}.country_code in ('JP', 'SG', 'PH', 'HK', 'TW', 'ID', 'MY', 'TH') ;; label: "Asia Core" }
      when: { sql: ${TABLE}.country_code in ('IN', 'PK') ;; label: "India, Pakistan" }
      when: { sql: ${TABLE}.country_code in ('RU') ;; label: "Russia" }
      when: { sql: ${TABLE}.country_code in ('AM', 'AZ', 'BY', 'GE', 'KZ', 'KG', 'MD', 'TM', 'TJ', 'UA', 'UZ') ;; label: "CIS ex. Russia" }
      when: { sql: ${TABLE}.country_code in ('ZA', 'NG', 'KE') ;; label: "Africa Core" }
      when: { sql: ${TABLE}.continent_id = 4 ;; label: "Rest of Europe" }
      when: { sql: ${TABLE}.continent_id = 7 ;; label: "Rest of LatAm" }
      when: { sql: ${TABLE}.continent_id = 3 ;; label: "Rest of Asia" }
      when: { sql: ${TABLE}.continent_id = 1 ;; label: "Rest of Africa" }
      else: "Rest of World"
    }
  }

}
# Missing Fields: latitude, longitude, date_created, last_updated
