{% snapshot m_DWH_FXCD_NH_F_ELIGIBILITY %}
{{
    config(
      target_schema='dw_rar',
      alias = 'NH_F_ELIGIBILITY_MARS',
      unique_key=['ELIGIBILITY_ID'],
      strategy='check',
      check_cols=[
          'ELIGIBILITY_TYPE',
          'ELIGIBILITY_NAME',
          'LIMIT_PERCENTAGE',
          'LIMIT_AMT_MANUAL_FLAG',
          'LIMIT_AMT',
          'NCB_LMT_AMT_FLOOR',
          'RATING_THRESHOLD',
          'FKIT_SUBLIMIT_NAME',
          'ELIGIBILITY_ORDER',
          'LIMIT_DISTRIBUTION',
          'CTP_MIN_REQ_RATINGS',
          'LIMITS_CALCULATION',
          'ART_101_FLAG',
          'DEFINITION',
          'FOR_CP_FLAG',
          'FOR_CPG_FLAG',
          'OVERALL_LIMIT_FLAG',
          'IDENTIFIER'
      ],
      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_ELIGIBILITY"]
    )
}}

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(ELIGIBILITY_ID AS NUMBER(28,0)) AS ELIGIBILITY_ID,
    CAST(ELIGIBILITY_TYPE AS VARCHAR2(2 CHAR)) AS ELIGIBILITY_TYPE,
    CAST(ELIGIBILITY_NAME AS VARCHAR2(200 CHAR)) AS ELIGIBILITY_NAME,
    CAST(LIMIT_PERCENTAGE AS NUMBER(28,10)) AS LIMIT_PERCENTAGE,
    CAST({{ clean_boolean_fxcd('LIMIT_AMT_MANUAL_FLAG') }} AS CHAR(1 CHAR)) AS LIMIT_AMT_MANUAL_FLAG,
    CAST(LIMIT_AMT AS NUMBER(28,10)) AS LIMIT_AMT,
    CAST(NCB_LMT_AMT_FLOOR AS NUMBER(28,10)) AS NCB_LMT_AMT_FLOOR,
    CAST(RATING_THRESHOLD AS VARCHAR2(4 CHAR)) AS RATING_THRESHOLD,
    CAST(FKIT_SUBLIMIT_NAME AS VARCHAR2(30 CHAR)) AS FKIT_SUBLIMIT_NAME,
    CAST(ELIGIBILITY_ORDER AS NUMBER(28,0)) AS ELIGIBILITY_ORDER,
    CAST(LIMIT_DISTRIBUTION AS VARCHAR2(3 CHAR)) AS LIMIT_DISTRIBUTION,
    CAST(CTP_MIN_REQ_RATINGS AS NUMBER(28,0)) AS CTP_MIN_REQ_RATINGS,
    CAST({{ clean_boolean_fxcd('LIMITS_CALCULATION') }} AS CHAR(1 CHAR)) AS LIMITS_CALCULATION,
    CAST({{ clean_boolean_fxcd('ART_101_FLAG') }} AS CHAR(1 CHAR)) AS ART_101_FLAG,
    CAST(DEFINITION AS VARCHAR2(50 CHAR)) AS DEFINITION,
    CAST(FOR_CP_FLAG AS VARCHAR2(1 CHAR)) AS FOR_CP_FLAG,
    CAST(FOR_CPG_FLAG AS VARCHAR2(1 CHAR)) AS FOR_CPG_FLAG,
    CAST(OVERALL_LIMIT_FLAG AS VARCHAR2(1 CHAR)) AS OVERALL_LIMIT_FLAG,
    CAST(IDENTIFIER AS VARCHAR2(6 CHAR)) AS IDENTIFIER
FROM
    {{ source('ods', 'F_ELIGIBILITY') }}
WHERE 
    A_WORKFLOW_HISTORY_KEY = {{ filter_workflow_history_max_key("w_ODS_FXCD_F_ELIGIBILITY",get_main_task_name(model.name))}}
{% endsnapshot %}