# ==============================================================================
# LAKELOGIC DATA CONTRACT — BRONZE (UNSTRUCTURED)
# ==============================================================================
# Name: {{ contract_name }}
# Generated by: Zeus AI 
# Template: bronze_unstructured
# Description: {{ description }}

version: "1.0"
tier: bronze
dataset: {{ dataset | default(contract_name) }}

# ── Metadata ──────────────────────────────────────────────────────────────────
info:
  owner: {{ owner | default('data-engineering@lakelogic.io') }}
  description: {{ description }}

metadata:
  classification: "confidential"
  run_log_dir: "abfss://lakelogic@{{ storage_account }}.dfs.core.windows.net/logs/runs/{{ contract_name }}"

# ── Source (Landing Zone) ─────────────────────────────────────────────────────
source:
  type: storage
{% if source_type == 'file' %}
  path: "{{ source_path | default('abfss://landing@' + storage_account + '.dfs.core.windows.net/raw_files/') }}"
  format: binaryFile
  options:
    pathGlobFilter: "*.{{ file_extension | default('pdf') }}"
{% else %}
  path: "{{ source_path | default('abfss://landing@' + storage_account + '.dfs.core.windows.net/raw_text/') }}"
  format: parquet
{% endif %}
  load_mode: incremental

# ── LLM Extraction (Unstructured → Structured) ────────────────────────────────
extraction:
  provider: {{ llm_provider | default('openai') }}
  model: {{ llm_model | default('gpt-4o') }}
  temperature: 0.1
  prompt_template: >
    {{ prompt_template | indent(4) }}

{% if source_type == 'file' %}
  preprocessing:
    content_type: {{ file_extension | default('pdf') }}
    ocr:
      enabled: true
      engine: azure_di
    text_output_column: "_extracted_text"
{% else %}
  text_column: "{{ source_text_column | default('raw_text') }}"
{% endif %}

  # The exact schema we expect the LLM to extract
  output_schema:
{% for col in output_columns %}
    - name: {{ col.name }}
      type: {{ col.type }}
      description: {{ col.description | default('Extracted from text') | tojson }}
{% endfor %}

# ── Target Schema (Extracted Fields) ──────────────────────────────────────────
model:
  fields:
{% for col in output_columns %}
    - name: {{ col.name }}
      type: {{ col.type }}
      description: {{ col.description | default('Extracted from text') | tojson }}
{% endfor %}
    - name: _lakelogic_extraction_confidence
      type: double
      description: "Confidence score generated by the extraction process"

# ── Quality Gates ─────────────────────────────────────────────────────────────
quality:
  enforce_required: false
  row_rules:
    - name: extraction_failed
      sql: "_lakelogic_extraction_confidence is null"
      severity: error
      description: "Quarantine rows where extraction failed completely"
{% for rule in quality_rules %}
    - name: {{ rule.name }}
      sql: {{ rule.sql | tojson }}
      severity: {{ rule.severity | default('error') }}
{% endfor %}

quarantine:
  enabled: true
  target: "abfss://lakelogic@{{ storage_account }}.dfs.core.windows.net/quarantine/{{ contract_name }}"

# ── Materialization ───────────────────────────────────────────────────────────
materialization:
  strategy: append
  format: delta
  reprocess_policy: overwrite_partition
  location: "abfss://lakelogic@{{ storage_account }}.dfs.core.windows.net/bronze/{{ contract_name }}"
