config {
  type: "table",
  schema: "{{ dataset_id or 'marts' }}",
  name: "{{ table_name }}",
  {%- if partition_by %}
  bigquery: {
    partitionBy: "{{ partition_by }}",
    {%- if cluster_by %}
    clusterBy: {{ cluster_by | tojson }},
    {%- endif %}
  },
  {%- endif %}
  {%- if tags %}
  tags: {{ tags | tojson }},
  {%- endif %}
  {%- if description %}
  description: "{{ description }}",
  {%- endif %}
  {%- if dependencies %}
  dependencies: {{ dependencies | tojson }},
  {%- endif %}
}

-- Aggregate/Mart Table: {{ table_name }}
-- Layer: MARTS (Platinum)
-- Purpose: Business-optimized aggregate for reporting
-- Generated by: dataform-warehouse-blueprints v0.1.0

SELECT
  -- Grouping Dimensions
{%- for key in primary_keys %}
  {{ key }},
{%- endfor %}
  
  -- Aggregated Measures
{%- for column in columns %}
  {%- if column not in primary_keys %}
  {{ column }}{{ "," if not loop.last else "" }}
  {%- endif %}
{%- endfor %}
  
  -- Audit
  CURRENT_TIMESTAMP() AS _refreshed_at
  
FROM {{ source_table }}

{%- if primary_keys %}

GROUP BY
{%- for key in primary_keys %}
  {{ key }}{{ "," if not loop.last else "" }}
{%- endfor %}

{%- endif %}

