{% snapshot m_DWH_FXCD_NH_F_COUNTERPARTY %}
{{
    config(
      target_schema='dw_rar',
      alias = 'NH_F_COUNTERPARTY_MARS',
      unique_key=['CTP_ID'],
      strategy='check',
      check_cols=['ENTITY_ID',
                  'CTP_GROUP_FKIT_CODE',
                  'ACTIVE_FLAG',
                  'CTP_COMMENT',
                  'GUARANTOR_ID',
                  'OF_OVERALL_LMT_AMT',
                  'OF_MANUAL_FLAG',
                  'FR_OVERALL_LMT_AMT',
                  'FR_MANUAL_FLAG',
                  'MP_OVERALL_LMT_AMT',
                  'MP_MANUAL_FLAG',
                  'EOI_CTP_GROUP',
                  'ART_101_FLAG',
                  'MEDIAN_CAPITAL_FLAG',
                  'CHANGE_DESCRIPTION'],
      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_COUNTERPARTY"]
    )
}}
SELECT
   (SELECT COALESCE(MAX(A_KEY), 0) FROM {{ this }}) + ROW_NUMBER() OVER (ORDER BY 1) as A_KEY,
   {{ get_workflow_history_key() }} AS A_DWH_LOAD_SET_FK,
   CTP_ID,
   ENTITY_ID,
   CTP_GROUP_FKIT_CODE,
   {{clean_boolean_fxcd('ACTIVE_FLAG')}} AS ACTIVE_FLAG,
   CTP_COMMENT, 
   GUARANTOR_ID,
   OF_OVERALL_LMT_AMT,
   {{clean_boolean_fxcd('OF_MANUAL_FLAG')}} AS OF_MANUAL_FLAG,
   FR_OVERALL_LMT_AMT,
   {{clean_boolean_fxcd('FR_MANUAL_FLAG')}} AS FR_MANUAL_FLAG,
   MP_OVERALL_LMT_AMT,
   {{clean_boolean_fxcd('MP_MANUAL_FLAG')}} AS MP_MANUAL_FLAG,
   EOI_CTP_GROUP,
   {{clean_boolean_fxcd('ART_101_FLAG')}} AS ART_101_FLAG,
   {{clean_boolean_fxcd('MEDIAN_CAPITAL_FLAG')}} AS MEDIAN_CAPITAL_FLAG,
   CHANGE_DESCRIPTION
FROM {{ source('ods', 'F_COUNTERPARTY') }}
WHERE 
    A_WORKFLOW_HISTORY_KEY = {{ filter_workflow_history_max_key("w_ODS_FXCD_F_COUNTERPARTY",get_main_task_name(model.name))}}
{% endsnapshot %}

