{% snapshot m_DWH_FXCD_NH_F_ISSUER %}
{{
    config(
      target_schema='dw_rar',
      alias = 'NH_F_ISSUER_MARS',
      unique_key=['ISSUER_ID'],
      strategy='check',
      check_cols=[
          'ENTITY_ID',
          'ACTIVE_FLAG',
          'ISSUER_COMMENT',
          'OFM_ISSUER_LMT',
          'OF_MANUAL_FLAG',
          'GLOBAL_LMT_AMNT',
          'MANUAL_GLOBAL_LMT_FLAG',
          'OF_ISSUER_CATEGORY',
          'FR_ISSUER_CATEGORY',
          'ISSUER_LMT',
          'FR_MANUAL_FLAG',
          'NCB_USD_LAMBDA',
          'ART_101_FLAG',
          'UPLIFT_IMPLIED_RATING',
          'LAMBDA_MANUAL_FLAG',
          'CHANGE_DESCRIPTION',
          'LT_FRM_LIMIT_RATIO',
          'LT_FRM_LIMIT_RATIO_MANUAL_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_ISSUER"]
    )
}}

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(ISSUER_ID AS NUMBER(28,0)) AS ISSUER_ID,
    CAST(ENTITY_ID AS NUMBER(28,0)) AS ENTITY_ID,
    CAST({{ clean_boolean_fxcd('ACTIVE_FLAG') }} AS CHAR(1 CHAR)) AS ACTIVE_FLAG,
    CAST(ISSUER_COMMENT AS VARCHAR2(4000 CHAR)) AS ISSUER_COMMENT,
    CAST(OFM_ISSUER_LMT AS NUMBER(28,10)) AS OFM_ISSUER_LMT,
    CAST({{ clean_boolean_fxcd('OF_MANUAL_FLAG') }} AS VARCHAR2(1 CHAR)) AS OF_MANUAL_FLAG,
    CAST(GLOBAL_LMT_AMNT AS NUMBER(28,10)) AS GLOBAL_LMT_AMNT,
    CAST({{ clean_boolean_fxcd('MANUAL_GLOBAL_LMT_FLAG') }} AS CHAR(1 CHAR)) AS MANUAL_GLOBAL_LMT_FLAG,
    CAST(OF_ISSUER_CATEGORY AS VARCHAR2(5 CHAR)) AS OF_ISSUER_CATEGORY,
    CAST(FR_ISSUER_CATEGORY AS VARCHAR2(5 CHAR)) AS FR_ISSUER_CATEGORY,
    CAST(ISSUER_LMT AS NUMBER(28,10)) AS ISSUER_LMT,
    CAST({{ clean_boolean_fxcd('FR_MANUAL_FLAG') }} AS CHAR(1 CHAR)) AS FR_MANUAL_FLAG,
    CAST(NCB_USD_LAMBDA AS NUMBER(28,10)) AS NCB_USD_LAMBDA,
    CAST(ART_101_FLAG AS CHAR(1 CHAR)) AS ART_101_FLAG,
    CAST(UPLIFT_IMPLIED_RATING AS VARCHAR2(4 CHAR)) AS UPLIFT_IMPLIED_RATING,
    CAST({{ clean_boolean_fxcd('LAMBDA_MANUAL_FLAG') }} AS VARCHAR2(1 CHAR)) AS LAMBDA_MANUAL_FLAG,
    CAST(CHANGE_DESCRIPTION AS VARCHAR2(256 CHAR)) AS CHANGE_DESCRIPTION,
    CAST(LT_FRM_LIMIT_RATIO AS NUMBER(28,10)) AS LT_FRM_LIMIT_RATIO,
    CAST({{ clean_boolean_fxcd('LT_FRM_LIMIT_RATIO_MANUAL_FLAG') }} AS VARCHAR2(1 CHAR)) AS LT_FRM_LIMIT_RATIO_MANUAL_FLAG
FROM
    {{ source('ods', 'F_ISSUER') }}
WHERE
    A_WORKFLOW_HISTORY_KEY = {{ filter_workflow_history_max_key("w_ODS_FXCD_F_ISSUER",get_main_task_name(model.name))}}
{% endsnapshot %}