{% snapshot m_DWH_FXCD_NH_F_RATING_AGENCY %}
{{
    config(
      target_schema='dw_rar',
      alias = 'NH_F_RATING_AGENCY_MARS',
      unique_key=['AGENCY_ID'],
      strategy='check',
      check_cols=[
          'AGENCY_NAME',
          'ENABLED_FLAG'
      ],
      hard_deletes='invalidate',
      dbt_valid_to_current="TIMESTAMP '9999-12-31 00:00:00'",
      snapshot_meta_column_names={
        'dbt_valid_from': 'A_VALID_FROM',
        'dbt_valid_to': 'A_VALID_TO',
        'dbt_updated_at': 'UPDATED_AT',
        'dbt_scd_id': 'SCD_ID'
      },
      column_types={
        'A_VALID_FROM': 'DATE',
        'A_VALID_TO': 'DATE'
      },
      tags=["m_DWH_FXCD_NH_F_RATING_AGENCY"]
    )
}}

SELECT DISTINCT
    (SELECT COALESCE(MAX(A_KEY), 0) FROM {{ this }}) + ROW_NUMBER() OVER (ORDER BY 1) as A_KEY,
    CAST({{ get_workflow_history_key() }} AS NUMBER(38,0)) AS A_DWH_LOAD_SET_FK,
    CAST(AGENCY_ID AS VARCHAR2(2 CHAR)) AS AGENCY_ID,
    CAST(AGENCY_NAME AS VARCHAR2(20 CHAR)) AS AGENCY_NAME,
    CAST({{ clean_boolean_fxcd('ENABLED_FLAG') }} AS VARCHAR2(1 CHAR)) AS ENABLED_FLAG
FROM
    {{ source('ods', 'F_RATING_AGENCY') }}
WHERE 
    A_WORKFLOW_HISTORY_KEY = {{ filter_workflow_history_max_key("w_ODS_FXCD_F_RATING_AGENCY",get_main_task_name(model.name))}}
{% endsnapshot %}