--=============================================================================================================================
-- MARS-1049-ADHOC: Recreate External Tables with UTF8 Encoding (Rollback Step)
--=============================================================================================================================
-- Purpose: Recreate CSDB external tables with UTF8 encoding (original encoding)
-- Author: Grzegorz Michalski
-- Date: 2025-11-26
-- Related: MARS-1049 (CSV Encoding Support), MARS-821 (Original CSDB Setup)
--
-- Tables to be created (18 total with UTF8 encoding):
--   CSDB_DEBT: INBOX, ODS, ARCHIVE
--   CSDB_DEBT_DAILY: INBOX, ODS, ARCHIVE
--   CSDB_INSTR_DESC_FULL: INBOX, ODS, ARCHIVE
--   CSDB_INSTR_RAT_FULL: INBOX, ODS, ARCHIVE  
--   CSDB_ISSUER_DESC_FULL: INBOX, ODS, ARCHIVE
--   CSDB_ISSUER_RAT_FULL: INBOX, ODS, ARCHIVE
--
-- Prerequisites:
--   - Template tables must exist in CT_ET_TEMPLATES schema
--   - FILE_MANAGER_ODS v2.1.0+ with pEncoding parameter support
--   - Proper bucket URIs configured in ENV_MANAGER
--=============================================================================================================================

SET SERVEROUTPUT ON

PROMPT ========================================================
PROMPT Step 1: Create CSDB_DEBT External Tables with UTF8
PROMPT ========================================================

-- INBOX - For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',
        pPrefix => 'INBOX/CSDB/CentralizedSecuritiesDissemination/CSDB_DEBT',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_DEBT_INBOX with encoding UTF8');
END;
/

-- ODS - For processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',
        pPrefix => 'ODS/CSDB/CSDB_DEBT',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_DEBT_ODS with encoding UTF8');
END;
/

-- ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',
        pPrefix => 'ARCHIVE/CSDB/CSDB_DEBT',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_DEBT_ARCHIVE with encoding UTF8');
END;
/

PROMPT ================================================================
PROMPT Step 2: Create CSDB_DEBT_DAILY External Tables with UTF8
PROMPT ================================================================

-- INBOX - For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_DAILY_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT_DAILY',
        pPrefix => 'INBOX/CSDB/CentralizedSecuritiesDailyReferenceDataDissemination/CSDB_DEBT_DAILY',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_DEBT_DAILY_INBOX with encoding UTF8');
END;
/

-- ODS - For processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_DAILY_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT_DAILY',
        pPrefix => 'ODS/CSDB/CSDB_DEBT_DAILY',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_DEBT_DAILY_ODS with encoding UTF8');
END;
/

-- ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_DAILY_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT_DAILY',
        pPrefix => 'ARCHIVE/CSDB/CSDB_DEBT_DAILY',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_DEBT_DAILY_ARCHIVE with encoding UTF8');
END;
/

PROMPT ========================================================================
PROMPT Step 3: Create CSDB_INSTR_DESC_FULL External Tables with UTF8
PROMPT ========================================================================

-- INBOX - For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_INSTR_DESC_FULL_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_INSTR_DESC_FULL',
        pPrefix => 'INBOX/CSDB/FullRatingsDissemination/CSDB_INSTR_DESC_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_INSTR_DESC_FULL_INBOX with encoding UTF8');
END;
/

-- ODS - For processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_INSTR_DESC_FULL_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_INSTR_DESC_FULL',
        pPrefix => 'ODS/CSDB/CSDB_INSTR_DESC_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_INSTR_DESC_FULL_ODS with encoding UTF8');
END;
/

-- ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_INSTR_DESC_FULL_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_INSTR_DESC_FULL',
        pPrefix => 'ARCHIVE/CSDB/CSDB_INSTR_DESC_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_INSTR_DESC_FULL_ARCHIVE with encoding UTF8');
END;
/

PROMPT ========================================================================
PROMPT Step 4: Create CSDB_INSTR_RAT_FULL External Tables with UTF8
PROMPT ========================================================================

-- INBOX - For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_INSTR_RAT_FULL_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_INSTR_RAT_FULL',
        pPrefix => 'INBOX/CSDB/FullRatingsDissemination/CSDB_INSTR_RAT_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_INSTR_RAT_FULL_INBOX with encoding UTF8');
END;
/

-- ODS - For processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_INSTR_RAT_FULL_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_INSTR_RAT_FULL',
        pPrefix => 'ODS/CSDB/CSDB_INSTR_RAT_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_INSTR_RAT_FULL_ODS with encoding UTF8');
END;
/

-- ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_INSTR_RAT_FULL_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_INSTR_RAT_FULL',
        pPrefix => 'ARCHIVE/CSDB/CSDB_INSTR_RAT_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_INSTR_RAT_FULL_ARCHIVE with encoding UTF8');
END;
/

PROMPT =========================================================================
PROMPT Step 5: Create CSDB_ISSUER_DESC_FULL External Tables with UTF8
PROMPT =========================================================================

-- INBOX - For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_ISSUER_DESC_FULL_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_ISSUER_DESC_FULL',
        pPrefix => 'INBOX/CSDB/FullRatingsDissemination/CSDB_ISSUER_DESC_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_ISSUER_DESC_FULL_INBOX with encoding UTF8');
END;
/

-- ODS - For processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_ISSUER_DESC_FULL_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_ISSUER_DESC_FULL',
        pPrefix => 'ODS/CSDB/CSDB_ISSUER_DESC_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_ISSUER_DESC_FULL_ODS with encoding UTF8');
END;
/

-- ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_ISSUER_DESC_FULL_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_ISSUER_DESC_FULL',
        pPrefix => 'ARCHIVE/CSDB/CSDB_ISSUER_DESC_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_ISSUER_DESC_FULL_ARCHIVE with encoding UTF8');
END;
/

PROMPT =========================================================================
PROMPT Step 6: Create CSDB_ISSUER_RAT_FULL External Tables with UTF8
PROMPT =========================================================================

-- INBOX - For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_ISSUER_RAT_FULL_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_ISSUER_RAT_FULL',
        pPrefix => 'INBOX/CSDB/FullRatingsDissemination/CSDB_ISSUER_RAT_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_ISSUER_RAT_FULL_INBOX with encoding UTF8');
END;
/

-- ODS - For processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_ISSUER_RAT_FULL_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_ISSUER_RAT_FULL',
        pPrefix => 'ODS/CSDB/CSDB_ISSUER_RAT_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_ISSUER_RAT_FULL_ODS with encoding UTF8');
END;
/

-- ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_ISSUER_RAT_FULL_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_ISSUER_RAT_FULL',
        pPrefix => 'ARCHIVE/CSDB/CSDB_ISSUER_RAT_FULL',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri,
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Created ODS.CSDB_ISSUER_RAT_FULL_ARCHIVE with encoding UTF8');
END;
/

PROMPT =========================================================================
PROMPT Recreate External Tables with UTF8 - COMPLETED
PROMPT =========================================================================
PROMPT 
PROMPT Summary:
PROMPT - CSDB_DEBT tables: 3 created (INBOX, ODS, ARCHIVE)
PROMPT - CSDB_DEBT_DAILY tables: 3 created (INBOX, ODS, ARCHIVE)
PROMPT - CSDB_INSTR_DESC_FULL tables: 3 created (INBOX, ODS, ARCHIVE)
PROMPT - CSDB_INSTR_RAT_FULL tables: 3 created (INBOX, ODS, ARCHIVE)
PROMPT - CSDB_ISSUER_DESC_FULL tables: 3 created (INBOX, ODS, ARCHIVE)
PROMPT - CSDB_ISSUER_RAT_FULL tables: 3 created (INBOX, ODS, ARCHIVE)
PROMPT - Total: 18 external tables created with UTF8 encoding
PROMPT 
PROMPT Next Step: Run 03_MARS_1049_ADHOC_verify_external_tables.sql
PROMPT =========================================================================
