--=============================================================================================================================
--Step 1: Create Template Table
--=============================================================================================================================

--Table LM_CURRENT_ACCOUNTS_HEADER
  CREATE TABLE "CT_ET_TEMPLATES"."LM_CURRENT_ACCOUNTS_HEADER" 
   ("A_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"A_WORKFLOW_HISTORY_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"REVISION_NUMBER" NUMBER(10,0), 
	"REFERENCE_DATE" DATE, 
	"FREE_TEXT" VARCHAR2(1000 CHAR),
	"CURRENT_ACCOUNT_BS_TOTAL" NUMBER(28,8), 
	"MRR_FORECAST_TOTAL" NUMBER(28,8), 
	"CURRENT_ACCOUNT_MESSAGE_TOTAL" NUMBER(28,8), 
	"MRR_MESSAGE_TOTAL" NUMBER(28,8), 
	"PERIODICITY" VARCHAR2(50 CHAR)
   ); 
/

--Table LM_CURRENT_ACCOUNTS_ITEM
CREATE TABLE "CT_ET_TEMPLATES"."LM_CURRENT_ACCOUNTS_ITEM" 
   ("A_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"A_WORKFLOW_HISTORY_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"A_HEADER_FK" NUMBER(38,0) NOT NULL ENABLE, 
	"COUNTRY" VARCHAR2(3 CHAR), 
	"MFI_CODE" VARCHAR2(255 CHAR), 
	"BANK_NAME" VARCHAR2(500 CHAR), 
	"CURRENT_ACCOUNT" NUMBER(28,8), 
	"MINIMUM_RESERVE_REQUIREMENT" NUMBER(28,8), 
	"COMMENT_" VARCHAR2(4000 CHAR)
   );
/

--=============================================================================================================================
--Step 2: Configure file type for processing
--=============================================================================================================================

--Table LM_CURRENT_ACCOUNTS_HEADER
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'MinimumReservesRequirements'
        ,pSourceFileDesc        => 'Table provides data on the account balance that credit institutions hold with their respective NCB within the Eurosystem. This is primarily used to hold minimum reserves.'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_CURRENT_ACCOUNTS_HEADER'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_HEADER'		
        ,pContainerFileKey => NULL
		);
/

--Table LM_CURRENT_ACCOUNTS_ITEM
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'MinimumReservesRequirements'   
        ,pSourceFileDesc        => 'Table provides data on the account balance that credit institutions hold with their respective NCB within the Eurosystem. This is primarily used to hold minimum reserves.'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_CURRENT_ACCOUNTS_ITEM'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_ITEM' 
		,pContainerFileKey => NULL
        );
/

--=============================================================================================================================
--Step 3: Date format adjustment
--=============================================================================================================================

--Table LM_CURRENT_ACCOUNTS_HEADER
CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_HEADER',    
	pColumnName => 'REFERENCE_DATE',    
	pDateFormat => 'YYYY-MM-DD'
	);
/

--=============================================================================================================================
--Step 4: Create External Tables
--=============================================================================================================================

--Table LM_CURRENT_ACCOUNTS_HEADER

--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CURRENT_ACCOUNTS_HEADER_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_HEADER',
        pPrefix => 'INBOX/LM/MinimumReservesRequirements/LM_CURRENT_ACCOUNTS_HEADER',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri
    );
END;
/

--ODS - for processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CURRENT_ACCOUNTS_HEADER_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_HEADER',
        pPrefix => 'ODS/LM/LM_CURRENT_ACCOUNTS_HEADER',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
/ 

--ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CURRENT_ACCOUNTS_HEADER_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_HEADER',
        pPrefix => 'ARCHIVE/LM/LM_CURRENT_ACCOUNTS_HEADER',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri
    );
END;
/

---------------------------------------------------------------
  --Table LM_CURRENT_ACCOUNTS_ITEM
  
--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CURRENT_ACCOUNTS_ITEM_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_ITEM',
        pPrefix => 'INBOX/LM/MinimumReservesRequirements/LM_CURRENT_ACCOUNTS_ITEM',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri
    );
END;
/

--ODS - for processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CURRENT_ACCOUNTS_ITEM_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_ITEM',
        pPrefix => 'ODS/LM/LM_CURRENT_ACCOUNTS_ITEM',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
/

--ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CURRENT_ACCOUNTS_ITEM_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CURRENT_ACCOUNTS_ITEM',
        pPrefix => 'ARCHIVE/LM/LM_CURRENT_ACCOUNTS_ITEM',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri
    );
END; 
/

--=============================================================================================================================
--Step 5: Create Compatibility View
--=============================================================================================================================

--View MRR_IND_CURRENT_ACCOUNT_HEADER_MARS
CREATE OR REPLACE VIEW OU_MRR.MRR_IND_CURRENT_ACCOUNT_HEADER_MARS AS    
	SELECT    
		REVISION_NUMBER,
		REFERENCE_DATE,
		FREE_TEXT,
		CURRENT_ACCOUNT_BS_TOTAL,
		MRR_FORECAST_TOTAL,
		CURRENT_ACCOUNT_MESSAGE_TOTAL,
		MRR_MESSAGE_TOTAL,
		PERIODICITY,
		A_KEY,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_KEY
	FROM ODS.LM_CURRENT_ACCOUNTS_HEADER_ODS;
/

--View MRR_IND_CURRENT_ACCOUNT_ITEM_MARS
CREATE OR REPLACE VIEW OU_MRR.MRR_IND_CURRENT_ACCOUNT_ITEM_MARS AS    
	SELECT    
		COUNTRY,
		MFI_CODE,
		BANK_NAME,
		CURRENT_ACCOUNT,
		MINIMUM_RESERVE_REQUIREMENT,
		COMMENT_,
		A_KEY,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_KEY,
		A_HEADER_FK
	FROM ODS.LM_CURRENT_ACCOUNTS_ITEM_ODS;
/