view: dt_restrictions_label_territory {
  sql_table_name: INTELLIGENCE.DBT_PROD.RESTRICTIONS_LABEL_TERRITORY;;
#  derived_table: {
#    sql: WITH territory_restrictions AS (
#  SELECT id,
#      TRY_TO_NUMBER(a.value::string) AS country_id
#  FROM orchard_app_reporting_v2.art_relations_prod_art_relations.vendor_contract,
#  LATERAL FLATTEN(INPUT => SPLIT(territory_carve_out, ',')) a
#  WHERE country_id IS NOT NULL)
#
#SELECT
#    vw.vendor_id,
#    LISTAGG(DISTINCT c.name, ', ') WITHIN GROUP (ORDER BY c.name) AS list_aggregate_restrictions
#FROM territory_restrictions tr
#INNER JOIN intelligence.dbt_prod.active_vendor_contracts vw ON tr.id = vw.vendor_contract_id
#INNER JOIN orchard_app_reporting_v2.art_relations_prod_art_relations.country c ON tr.country_id = c.id
#GROUP BY 1
#      ;;
#  }

  dimension: label_id {
    type: number
    sql: ${TABLE}.vendor_id ;;
    hidden: yes
  }


  dimension: list_aggregate_restrictions {
    label: "Restrictions by Country (List)"
    type: string
    sql: ${TABLE}.list_aggregate_restrictions ;;
    html:
    {% assign countries = value | split: "," %}
    {% unless countries.size == 0 %}

      <details>
      <summary>Detail ({{countries.size}})</summary>

      <ul>
      {% for country in countries %}
      <li>{{country}}</li>
      {% endfor %}
      </ul>
      </details>

    {% endunless %}
    ;;
  }

}