# ============================================================
# LAKELOGIC DATA CONTRACT — GOLD LAYER
# Generated by Zeus AI from template: gold_contract
# ============================================================

version: "1.0.0"

info:
  title: "{{ title }}"
  version: "1.0.0"
  description: "{{ description }}"
  target_layer: "gold"
{%- if domain %}
  domain: "{{ domain }}"
{%- endif %}
{%- if system %}
  system: "{{ system }}"
{%- endif %}

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

# ── Upstream Silver Contracts ──────────────────────────────────
upstream:
{%- for source in silver_sources %}
  - "{{ source.name }}"
{%- endfor %}

# ── Source Configuration ───────────────────────────────────────
source:
  type: silver_layer
  path: "{{ silver_sources[0].path | default('') }}"

{%- if silver_sources | length > 1 %}

# ── Reference Links (additional Silver tables) ────────────────
links:
{%- for source in silver_sources[1:] %}
  - name: "{{ source.ref_name }}"
    path: "{{ source.path | default('') }}"
    type: "{{ source.format | default('delta') }}"
{%- endfor %}
{%- endif %}

# ── External Logic (processor) ────────────────────────────────
external_logic:
  type: python
  path: "{{ processor_path }}"
  entrypoint: "run"
{%- if silver_sources | length > 1 %}
  args:
{%- for source in silver_sources[1:] %}
    {{ source.param_name }}: "{{ source.table_ref | default('') }}"
{%- endfor %}
{%- endif %}

# ── Schema Model ──────────────────────────────────────────────
model:
  fields:
{%- for field in output_fields %}
    - name: "{{ field.name }}"
      type: "{{ field.type }}"
      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 %}

# ── Quality Rules ─────────────────────────────────────────────
quality:
  enforce_required: true
{%- if row_rules %}
  row_rules:
{%- for rule in row_rules %}
{%- if rule.type == 'sql' %}
    - name: "{{ rule.name }}"
      sql: "{{ rule.sql }}"
      category: "{{ rule.category | default('correctness') }}"
{%- if rule.description %}
      description: "{{ rule.description }}"
{%- endif %}
{%- 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 == 'not_null' %}
    - not_null: "{{ rule.field }}"
{%- endif %}
{%- endfor %}
{%- endif %}
{%- if dataset_rules %}
  dataset_rules:
{%- for rule in dataset_rules %}
{%- if rule.type == 'unique' %}
    - unique: "{{ rule.field }}"
{%- elif rule.type == 'row_count_between' %}
    - row_count_between:
        min: {{ rule.min }}
{%- if rule.max is defined %}
        max: {{ rule.max }}
{%- endif %}
{%- elif rule.type == 'sql' %}
    - name: "{{ rule.name }}"
      sql: "{{ rule.sql }}"
      must_be_greater_than: {{ rule.threshold }}
{%- endif %}
{%- endfor %}
{%- endif %}

# ── 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
