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

--Table LM_STANDING_FACILITIES
 CREATE TABLE   "CT_ET_TEMPLATES"."LM_STANDING_FACILITIES" 
   ("A_KEY" NUMBER(38,0) NOT NULL ENABLE,
	"A_WORKFLOW_HISTORY_KEY" NUMBER(38,0) NOT NULL ENABLE,
	"A_SFH_FK" NUMBER(38,0) NOT NULL ENABLE, 
	"COUNTRY" VARCHAR2(12 BYTE), 
	"MFI_ID" VARCHAR2(1020 BYTE), 
	"MFI_NAME" VARCHAR2(2000 BYTE), 
	"MARGINAL_LENDING_FACILITY" NUMBER(28,10), 
	"DEPOSIT_FACILITY" NUMBER(28,10), 
	"COMMENT_" VARCHAR2(16000 BYTE)
   );
/

--Table STANDING_FACILITIES_HEADER
CREATE TABLE "CT_ET_TEMPLATES"."LM_STANDING_FACILITIES_HEADER" 
   ("A_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"A_WORKFLOW_HISTORY_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"REV_NUMBER" NUMBER(28,0), 
	"REF_DATE" DATE, 
	"FREE_TEXT" VARCHAR2(4000 BYTE), 
	"MLF_BS_TOTAL" NUMBER(28,10), 
	"DF_BS_TOTAL" NUMBER(28,10), 
	"MLF_SF_TOTAL" NUMBER(28,10), 
	"DF_SF_TOTAL" NUMBER(28,10)
	);
/
--=============================================================================================================================
--Step 2: Configure file type for processing
--=============================================================================================================================

--Table LM_STANDING_FACILITIES
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'DistributeStandingFacilities'
        ,pSourceFileDesc        => 'Table provides data on the credit facility available to counterparties at their own initiative. The Eurosystem offers two overnight standing facilities: the marginal lending facility and the deposit facility'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_STANDING_FACILITIES'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_STANDING_FACILITIES'		
        ,pContainerFileKey => NULL
		);
/

--Table LM_STANDING_FACILITIES_HEADER
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'DistributeStandingFacilities'
        ,pSourceFileDesc        => 'Table provides data on the credit facility available to counterparties at their own initiative. The Eurosystem offers two overnight standing facilities: the marginal lending facility and the deposit facility'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_STANDING_FACILITIES_HEADER'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_STANDING_FACILITIES_HEADER' 
		,pContainerFileKey => NULL
        );
/
--=============================================================================================================================
--Step 3: Date format adjustment
--=============================================================================================================================

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

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

--Table LM_STANDING_FACILITIES

--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_STANDING_FACILITIES_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_STANDING_FACILITIES',
        pPrefix => 'INBOX/LM/DistributeStandingFacilities/LM_STANDING_FACILITIES',
        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_STANDING_FACILITIES_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_STANDING_FACILITIES',
        pPrefix => 'ODS/LM/LM_STANDING_FACILITIES',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
/ 

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

---------------------------------------------------------------
  --Table LM_STANDING_FACILITIES_HEADER
  
--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_STANDING_FACILITIES_HEADER_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_STANDING_FACILITIES_HEADER',
        pPrefix => 'INBOX/LM/DistributeStandingFacilities/LM_STANDING_FACILITIES_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_STANDING_FACILITIES_HEADER_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_STANDING_FACILITIES_HEADER',
        pPrefix => 'ODS/LM/LM_STANDING_FACILITIES_HEADER',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
/

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

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

--View STANDING_FACILITIES_MARS
CREATE OR REPLACE VIEW OU_LM.STANDING_FACILITIES_MARS AS    
	SELECT    
		A_KEY,
		A_SFH_FK,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_FK,
		COUNTRY,
		MFI_ID,
		MFI_NAME,
		MARGINAL_LENDING_FACILITY,
		DEPOSIT_FACILITY,
		COMMENT_
	FROM ODS.LM_STANDING_FACILITIES_ODS;
/

--View STANDING_FACILITIES_HEADER_MARS
CREATE OR REPLACE VIEW OU_LM.STANDING_FACILITIES_HEADER_MARS AS
    SELECT    
        A_KEY,
        A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_FK,
        REV_NUMBER,
        REF_DATE,
        FREE_TEXT,
        MLF_BS_TOTAL,
        DF_BS_TOTAL,
        MLF_SF_TOTAL,
        DF_SF_TOTAL
    FROM ODS.LM_STANDING_FACILITIES_HEADER_ODS;
/