-- This file may be outdated because we actually use the `fal` materialization
{% materialization table, adapter='fal_experimental', supported_languages=['python'] -%}

  -- TODO: change to sync_teleport() when available in dbt-core
  {%- if adapter.is_teleport() -%}
    {%- for _ref in model.refs -%}
        {%- set resolved = ref(*_ref) -%}
        {%- do adapter.sync_teleport_relation(ref(*_ref)) -%}
    {%- endfor -%}
  {%- endif -%}

  {%- set relation = this.incorporate(type='table') -%}

  {%- call statement('main', language='python') -%}

    {{- py_write(compiled_code, relation) }}

  {%- endcall %}

  {{- return({'relations': [relation]}) }}

{% endmaterialization %}

{% macro py_write(code, relation) -%}
{{- compiled_code -}}

# Generated by dbt-fal

def main(read_df, write_df, fal_context=None):
  dbt_context = dbtObj(read_df)
  df = model(dbt_context, fal_context)
  return write_df(
      '{{ relation.quote(False, False, False) }}',
      df
  )
{%- endmacro %}
