# ============================================================
# LAKELOGIC DATA CONTRACT — SILVER LAYER
# Generated by Contract Studio from template: silver_cleansed
# ============================================================

version: "1.0.0"

info:
  title: "{{ title }}"
  version: "1.0.0"
  description: "{{ description | default('Cleansed & typed contract — Silver layer') }}"
  target_layer: "silver"
  domain: "{{ domain }}"
  system: "{{ system }}"

dataset: "{{ dataset_name }}"
primary_key: {{ primary_key | tojson }}

{%- if upstream %}
# ── Upstream Bronze ────────────────────────────────────────────
upstream:
{%- for u in upstream %}
  - "{{ u }}"
{%- endfor %}
{%- endif %}

# ── Source Configuration ───────────────────────────────────────
source:
  type: "{{ source_type | default('delta') }}"
  path: "{{ source_path | default('') }}"

# ── Schema Model ──────────────────────────────────────────────
model:
  fields:
{%- for field in fields %}
    - name: "{{ field.name }}"
      type: "{{ field.type | default('string') }}"
      required: {{ field.required | default(false) | tojson }}
{%- if field.pii is defined and field.pii %}
      pii: true
      classification: "{{ field.classification | default('confidential') }}"
{%- endif %}
{%- if field.description %}
      description: "{{ field.description }}"
{%- endif %}
{%- if field.accepted_values %}
      accepted_values: {{ field.accepted_values | tojson }}
{%- endif %}
{%- if field.min is defined %}
      min: {{ field.min }}
{%- endif %}
{%- if field.max is defined %}
      max: {{ field.max }}
{%- endif %}
{%- endfor %}

# ── Schema Policy ─────────────────────────────────────────────
schema_policy:
  unknown_fields: "{{ schema_policy_unknown | default('quarantine') }}"

# ── Quality Rules ─────────────────────────────────────────────
quality:
  enforce_required: true
{%- if row_rules %}
  row_rules:
{%- for rule in row_rules %}
{%- if rule.type == 'not_null' %}
    - not_null: "{{ rule.field }}"
{%- elif rule.type == 'range' %}
    - range:
        field: "{{ rule.field }}"
        min: {{ rule.min }}
        max: {{ rule.max }}
      category: "{{ rule.category | default('correctness') }}"
{%- elif rule.type == 'accepted_values' %}
    - accepted_values:
        field: "{{ rule.field }}"
        values: {{ rule.values | tojson }}
      category: "{{ rule.category | default('consistency') }}"
{%- elif rule.type == 'sql' %}
    - name: "{{ rule.name }}"
      sql: "{{ rule.sql }}"
      category: "{{ rule.category | default('correctness') }}"
{%- endif %}
{%- endfor %}
{%- endif %}

  dataset_rules:
{%- for pk in primary_key %}
    - unique: "{{ pk }}"
{%- endfor %}
{%- if row_count_min is defined %}
    - row_count_between:
        min: {{ row_count_min }}
{%- endif %}

{%- if dedup_keys %}

# ── Transformations (Deduplication) ───────────────────────────
transformations:
  - phase: pre
    deduplicate:
      on: {{ dedup_keys | tojson }}
{%- if dedup_sort_by %}
      sort_by: {{ dedup_sort_by | tojson }}
      order: "{{ dedup_order | default('desc') }}"
{%- endif %}
{%- endif %}

# ── Quarantine ────────────────────────────────────────────────
quarantine:
  enabled: true
  include_error_reason: true

# ── Materialization ───────────────────────────────────────────
materialization:
  strategy: "{{ materialization_strategy | default('overwrite') }}"
  format: "{{ materialization_format | default('delta') }}"
  target_path: "{{ target_path | default('') }}"
{%- if partition_by %}
  partition_by: {{ partition_by | tojson }}
{%- endif %}

# ── Lineage ───────────────────────────────────────────────────
lineage:
  enabled: true
  capture_source_path: true
  capture_timestamp: true
  capture_run_id: true
  capture_contract_name: true
