{% snapshot m_DWH_FXCD_NH_F_ENTITY %}
{{
    config(
      target_schema='dw_rar',
      alias = 'NH_F_ENTITY_MARS',
      unique_key=['ENTITY_ID'],
      strategy='check',
      check_cols=[
          'FKIT_CODE',
          'ENTITY_NAME',
          'BVD_BANK_INDEX_NUMBER',
          'RISK_COUNTRY_ID',
          'LT_MANUAL_RATING_FLAG',
          'LT_SECOND_BEST_RATING',
          'CAP_TIER1_AMT',
          'CAP_EQUITY_AMT',
          'BLOOMBERG_TICKER'
      ],
      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_ENTITY"]
    )
}}

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(ENTITY_ID AS NUMBER(28,0)) AS ENTITY_ID,
    CAST(FKIT_CODE AS VARCHAR2(20 CHAR)) AS FKIT_CODE,
    CAST(ENTITY_NAME AS VARCHAR2(200 CHAR)) AS ENTITY_NAME,
    CAST(BVD_BANK_INDEX_NUMBER AS VARCHAR2(100 CHAR)) AS BVD_BANK_INDEX_NUMBER,
    CAST(RISK_COUNTRY_ID AS CHAR(2 BYTE)) AS RISK_COUNTRY_ID,
    CAST(NULL AS CHAR(1 CHAR)) AS ST_MANUAL_RATING_FLAG, -- Not mapped in Informatica target, so excluded from check_cols
    CAST({{ clean_boolean_fxcd('LT_MANUAL_RATING_FLAG') }} AS CHAR(1 CHAR)) AS LT_MANUAL_RATING_FLAG,
    CAST(NULL AS VARCHAR2(4 CHAR)) AS ST_SECOND_BEST_RATING, -- Not mapped in Informatica target, so excluded from check_cols
    CAST(LT_SECOND_BEST_RATING AS VARCHAR2(4 CHAR)) AS LT_SECOND_BEST_RATING,
    CAST(CAP_TIER1_AMT AS NUMBER(28,10)) AS CAP_TIER1_AMT,
    CAST(CAP_EQUITY_AMT AS NUMBER(28,10)) AS CAP_EQUITY_AMT,
    CAST(BLOOMBERG_TICKER AS VARCHAR2(20 CHAR)) AS BLOOMBERG_TICKER
FROM
    {{ source('ods', 'F_ENTITY') }}
WHERE 
    A_WORKFLOW_HISTORY_KEY = {{ filter_workflow_history_max_key("w_ODS_FXCD_F_ENTITY",get_main_task_name(model.name))}}
{% endsnapshot %}