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

--table old CSM_ADJ_HEADER -> new LM_CSM_ADJUSTMENTS_HEADER
CREATE TABLE "CT_ET_TEMPLATES"."LM_CSM_ADJUSTMENTS_HEADER" 
   ("A_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"A_WORKFLOW_HISTORY_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"YEAR" VARCHAR2(4 BYTE) COLLATE "USING_NLS_COMP", 
	"MONTH" VARCHAR2(2 BYTE) COLLATE "USING_NLS_COMP", 
	"VERSION" NUMBER(10,0)
   );
/

--table old CSM_ADJ_ITEM -> new LM_CSM_ADJUSTMENTS_ITEM
  CREATE TABLE "CT_ET_TEMPLATES"."LM_CSM_ADJUSTMENTS_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, 
	"FORECAST_NAME" VARCHAR2(50 BYTE) COLLATE "USING_NLS_COMP", 
	"ADJUSTMENT_AMOUNT" VARCHAR2(50 BYTE) --   !!!!  due to incomming data containing comma 
	--"ADJUSTMENT_AMOUNT" NUMBER(25,8)
   );
/


--table old CSM_ADJ_ITEM_HEADER - > new LM_CSM_ADJUSTMENTS_ITEM_HEADER
CREATE TABLE "CT_ET_TEMPLATES"."LM_CSM_ADJUSTMENTS_ITEM_HEADER" 
   ("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 BYTE) COLLATE "USING_NLS_COMP", 
	"EFFECTIVE_DATE" DATE, 
	"LAST_DATE_NOT_FORECAST" DATE
   );
/

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

--Table LM_CSM_ADJUSTMENTS_HEADER
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'MonthlyCSMAdjustment'
        ,pSourceFileDesc        => 'Table provides the balance sheet with revised banknotes in line with monthly capital share mechanism.'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_CSM_ADJUSTMENTS_HEADER'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_CSM_ADJUSTMENTS_HEADER' 
        ,pContainerFileKey => NULL
		);
/

--Table LM_CSM_ADJUSTMENTS_ITEM
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'MonthlyCSMAdjustment'
        ,pSourceFileDesc        => 'Table provides the balance sheet with revised banknotes in line with monthly capital share mechanism.'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_CSM_ADJUSTMENTS_ITEM'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_CSM_ADJUSTMENTS_ITEM' 
        ,pContainerFileKey => NULL
		);
/       

--Table  LM_CSM_ADJUSTMENTS_ITEM_HEADER
CALL CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'LM'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'MonthlyCSMAdjustment'
        ,pSourceFileDesc        => 'Table provides the balance sheet with revised banknotes in line with monthly capital share mechanism.'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'LM_CSM_ADJUSTMENTS_ITEM_HEADER'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.LM_CSM_ADJUSTMENTS_ITEM_HEADER' 
        ,pContainerFileKey => NULL
		);
/

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


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

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

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

--Table LM_CSM_ADJUSTMENTS_HEADER

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

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

---------------------------------------------------------------
--Table LM_CSM_ADJUSTMENTS_ITEM
  
--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CSM_ADJUSTMENTS_ITEM_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CSM_ADJUSTMENTS_ITEM',
        pPrefix => 'INBOX/LM/MonthlyCSMAdjustment/LM_CSM_ADJUSTMENTS_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_CSM_ADJUSTMENTS_ITEM_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CSM_ADJUSTMENTS_ITEM',
        pPrefix => 'ODS/LM/LM_CSM_ADJUSTMENTS_ITEM',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
/

--ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CSM_ADJUSTMENTS_ITEM_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CSM_ADJUSTMENTS_ITEM',
        pPrefix => 'ARCHIVE/LM/LM_CSM_ADJUSTMENTS_ITEM',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri
    );
END; 
/
  
--------------------------------------------------------------------
--Table LM_CSM_ADJUSTMENTS_ITEM_HEADER 
 
--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'LM_CSM_ADJUSTMENTS_ITEM_HEADER_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CSM_ADJUSTMENTS_ITEM_HEADER',
        pPrefix => 'INBOX/LM/MonthlyCSMAdjustment/LM_CSM_ADJUSTMENTS_ITEM_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_CSM_ADJUSTMENTS_ITEM_HEADER_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.LM_CSM_ADJUSTMENTS_ITEM_HEADER',
        pPrefix => 'ODS/LM/LM_CSM_ADJUSTMENTS_ITEM_HEADER',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
/

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


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


--View CSM_ADJUSTMENTS_HEADER_MARS
CREATE OR REPLACE VIEW OU_LM.CSM_ADJ_HEADER_MARS AS    
	SELECT    
		A_KEY,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_KEY_FK,
		YEAR,
		MONTH,
		VERSION
	FROM ODS.LM_CSM_ADJUSTMENTS_HEADER_ODS;
/

--View CSM_ADJUSTMENTS_ITEM_MARS
CREATE OR REPLACE VIEW OU_LM.CSM_ADJ_ITEM_MARS AS    
	SELECT    
	A_KEY,
	A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_KEY_FK,
	A_HEADER_FK,
	FORECAST_NAME,
	ADJUSTMENT_AMOUNT
	FROM ODS.LM_CSM_ADJUSTMENTS_ITEM_ODS;
/

--View CSM_ADJUSTMENTS_ITEM_HEADER_MARS
CREATE OR REPLACE VIEW OU_LM.CSM_ADJ_ITEM_HEADER_MARS AS    
	SELECT    
		A_KEY,
		A_WORKFLOW_HISTORY_KEY AS A_ETL_LOAD_SET_KEY_FK,
		A_HEADER_FK,
		COUNTRY,
		EFFECTIVE_DATE,
		LAST_DATE_NOT_FORECAST
	FROM ODS.LM_CSM_ADJUSTMENTS_ITEM_HEADER_ODS;
/