version: 1

policy:
  # This is a security boundary, not a list of query templates or known schemas.
  allowed_tables:
    - gcp-digital-bi.metadb_new.*
  forbidden_functions:
    - EXTERNAL_QUERY
  require_fully_qualified_tables: true
  warn_on_select_star: true
  maximum_result_rows: 10000

datasets:
  metadb_new:
    project: gcp-digital-bi
    dataset: metadb_new
    location: us-west2
    description: Read-only metadata available to Manual Invoice; table schemas are discovered dynamically.

entities:
  # Entities are optional reviewed aliases/defaults. Tables do not need an entity entry to be queried.
  user_unit_costs:
    aliases:
      - user unit costs
      - user_unit_cost
      - user_unit_costs
      - monthly cost
      - monthly bill
      - 月账单
      - 月度账单
    table: gcp-digital-bi.metadb_new.user_unit_costs
    description: Preferred detailed amount source for monthly bill and monthly cost reconciliation.
    tenant_field: tenant_id
    default_time_field: cost_time_stamp
    time_fields:
      billing: cost_time_stamp
    dimensions:
      - unit
      - labels
      - consumed_source
      - invoice_id
      - internal
      - binded_billing_config_id
      - status
    numeric_fields:
      - cost_price
      - raw_price
    notes:
      - Inspect the live Schema before every query; this alias is a routing hint, not a frozen Schema.
      - Monthly period filtering must use cost_time_stamp with a half-open interval.
      - Apply references/user-unit-cost-calculation.md instead of an ordinary aggregate.
    defaults:
      source_priority: preferred_for_monthly_bill

  invoices:
    aliases:
      - invoice
      - invoices
      - 发票
      - 账单
    table: gcp-digital-bi.metadb_new.billing2_invoices
    description: Invoice records for a tenant.
    primary_key: id
    tenant_field: tenant_id
    default_time_field: sent_date
    time_fields:
      created: created_at
      updated: updated_at
      invoice: invoice_date
      sent: sent_date
    dimensions:
      - status
      - type
      - payment_type
      - payment_method_id
      - payment_code
      - parent_invoice_id
    numeric_fields:
      - cost_price
      - raw_price
      - channel_loss
    partition_field: updated_at
    notes:
      - Database metadata does not define the business meaning or currency of cost_price, raw_price, or channel_loss.
      - Preserve numeric field names in results unless a reviewed billing rule defines their meaning.
    defaults:
      monthly_metrics:
        - invoice_count
        - raw_price_sum
        - cost_price_sum
        - channel_loss_sum

metrics:
  invoice_count:
    expression: COUNT(*)
    description: Number of invoice rows.
  raw_price_sum:
    expression: SUM(raw_price)
    description: Sum of raw_price; business meaning and currency require a reviewed rule.
  cost_price_sum:
    expression: SUM(cost_price)
    description: Sum of cost_price; business meaning and currency require a reviewed rule.
  channel_loss_sum:
    expression: SUM(channel_loss)
    description: Sum of channel_loss; business meaning and currency require a reviewed rule.
