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

--Table LM_FORECAST_HEADER
 CREATE TABLE   "CT_ET_TEMPLATES"."LM_FORECAST_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, 
	"REVISION" NUMBER(10,0),
	"FREE_TEXT" VARCHAR2(4000 CHAR)
	);
/

--Table LM_FORECAST_ITEM
CREATE TABLE "CT_ET_TEMPLATES"."LM_FORECAST_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), 
	"FORECAST_DATE" DATE, 
	"FORECAST_NAME" VARCHAR2(50 CHAR), 
	"FORECAST_VALUE" NUMBER(28,10)
	);
/

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

--Table LM_FORECAST_HEADER
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'EurosystemAutonomousFactorForecast'
        ,pSourceFileDesc        => 'Table provides forecasts on the autonomous factors, monetary policy portfolios and reserve requirements. The data is updated weekly and disseminated daily'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_FORECAST_HEADER'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_FORECAST_HEADER'		
        ,pContainerFileKey => NULL
		);
/

--Table LM_FORECAST_ITEM
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'EurosystemAutonomousFactorForecast'   
        ,pSourceFileDesc        => 'Table provides forecasts on the autonomous factors, monetary policy portfolios and reserve requirements. The data is updated weekly and disseminated daily'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_FORECAST_ITEM'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_FORECAST_ITEM' 
		,pContainerFileKey => NULL
        );
/

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

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

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

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

--Table LM_FORECAST_HEADER

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

--ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_FORECAST_HEADER_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_FORECAST_HEADER',
        pPrefix => 'ARCHIVE/LM/LM_FORECAST_HEADER',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri
    );
END;
/
---------------------------------------------------------------
  --Table LM_FORECAST_ITEM
  
--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_FORECAST_ITEM_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_FORECAST_ITEM',
        pPrefix => 'INBOX/LM/EurosystemAutonomousFactorForecast/LM_FORECAST_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_FORECAST_ITEM_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_FORECAST_ITEM',
        pPrefix => 'ODS/LM/LM_FORECAST_ITEM',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
/

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


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

--View FORECAST_HEADER_MARS
CREATE OR REPLACE VIEW OU_LM.FORECAST_HEADER_MARS AS    
	SELECT    
		A_KEY,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_FK,
		COUNTRY,
		REFERENCE_DATE,
		REVISION,
		FREE_TEXT
	FROM ODS.LM_FORECAST_HEADER_ODS;
/

--View FORECAST_ITEM_MARS
CREATE OR REPLACE VIEW OU_LM.FORECAST_ITEM_MARS AS    
	SELECT    
		A_KEY,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_FK,
		A_HEADER_FK,
		FORECAST_DATE,
		FORECAST_NAME,
		FORECAST_VALUE
	FROM ODS.LM_FORECAST_ITEM_ODS;
/