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

--Table LM_BALANCESHEET_HEADER
CREATE TABLE "CT_ET_TEMPLATES"."LM_BALANCESHEET_HEADER" 
   ("A_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"A_WORKFLOW_HISTORY_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"COUNTRY" VARCHAR2(3 CHAR), 
	"REFERENCE_DATE" DATE, 
	"VERSION" NUMBER(10,0), 
	"STATUS" VARCHAR2(1 CHAR), 
	"FREE_TEXT" VARCHAR2(4000 CHAR)
   ); 
/


--Table LM_BALANCESHEET_ITEM
  CREATE TABLE "CT_ET_TEMPLATES"."LM_BALANCESHEET_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, 
	"ITEM_LEVEL" NUMBER(10,0), 
	"POSITION" NUMBER(10,0), 
	"ITEM_TYPE" VARCHAR2(10 CHAR), 
	"FULLY_QUALIFIED_POSITION" VARCHAR2(200 CHAR), 
	"NAME" VARCHAR2(200 CHAR), 
	"AMOUNT" NUMBER(28,8)
	);
/

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

--Table LM_BALANCESHEET_HEADER
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'AggregatedDailyFinancialStatementOfTheEurosystem'
        ,pSourceFileDesc        => 'Table provides data on the assets and liabilities of the ECB and Euro Area NCBs.'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_BALANCESHEET_HEADER'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_BALANCESHEET_HEADER' 
        ,pContainerFileKey      => NULL
		);
/

--Table LM_BALANCESHEET_ITEM
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'AggregatedDailyFinancialStatementOfTheEurosystem'
        ,pSourceFileDesc        => 'Table provides data on the assets and liabilities of the ECB and Euro Area NCBs.'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_BALANCESHEET_ITEM'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_BALANCESHEET_ITEM' 
        ,pContainerFileKey      => NULL
		);
/

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

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

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

--Table LM_BALANCESHEET_HEADER

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

--ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_BALANCESHEET_HEADER_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_BALANCESHEET_HEADER',
        pPrefix => 'ARCHIVE/LM/LM_BALANCESHEET_HEADER',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri
    );
END;
/
---------------------------------------------------------------
--Table LM_BALANCESHEET_ITEM
  
--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_BALANCESHEET_ITEM_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_BALANCESHEET_ITEM',
        pPrefix => 'INBOX/LM/AggregatedDailyFinancialStatementOfTheEurosystem/LM_BALANCESHEET_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_BALANCESHEET_ITEM_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_BALANCESHEET_ITEM',
        pPrefix => 'ODS/LM/LM_BALANCESHEET_ITEM',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
/

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

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

--View BALANCESHEET_HEADER_MARS
CREATE OR REPLACE VIEW OU_LM.BALANCESHEET_HEADER_MARS AS    
	SELECT    
		A_KEY,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_KEY,
		COUNTRY,
		REFERENCE_DATE,
		VERSION,
		STATUS,
		FREE_TEXT
	FROM ODS.LM_BALANCESHEET_HEADER_ODS;
/

--View BALANCESHEET_ITEM_MARS
CREATE OR REPLACE VIEW OU_LM.BALANCESHEET_ITEM_MARS AS    
	SELECT    
		A_KEY,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_KEY,
		A_HEADER_FK,
		ITEM_LEVEL,
		POSITION,
		ITEM_TYPE,
		FULLY_QUALIFIED_POSITION,
		NAME,
		AMOUNT
	FROM ODS.LM_BALANCESHEET_ITEM_ODS;
/