--=============================================================================================================================
-- MARS-1049-ADHOC: Drop External Tables for CSDB Encoding Change
--=============================================================================================================================
-- Purpose: Drop existing CSDB external tables before recreation with proper encoding parameter
-- Author: Grzegorz Michalski
-- Date: 2025-11-26
-- Related: MARS-1049 (CSV Encoding Support)
--
-- Tables to be dropped (16 total - existing and non-existing):
--   - CSDB_DEBT: ARCHIVE, INBOX, ODS
--   - CSDB_DEBT_DAILY: ARCHIVE, INBOX, ODS
--   - CSDB_INSTR_DESC_FULL: ARCHIVE, INBOX, ODS
--   - CSDB_INSTR_RAT_FULL: ARCHIVE, ODS
--   - CSDB_ISSUER_DESC_FULL: ARCHIVE, ODS
--   - CSDB_ISSUER_RAT_FULL: ARCHIVE, INBOX, ODS
--
-- Note: Script handles both existing and non-existing tables gracefully
--=============================================================================================================================

SET SERVEROUTPUT ON

DECLARE
    TYPE t_string_array IS TABLE OF VARCHAR2(100);
    
    -- Define list of tables to drop (16 total)
    vTableList t_string_array := t_string_array(
        'CSDB_DEBT_ARCHIVE',
        'CSDB_DEBT_INBOX',
        'CSDB_DEBT_ODS',
        
        'CSDB_DEBT_DAILY_ARCHIVE',
        'CSDB_DEBT_DAILY_INBOX',
        'CSDB_DEBT_DAILY_ODS',
        
        'CSDB_INSTR_DESC_FULL_ARCHIVE',
        'CSDB_INSTR_DESC_FULL_INBOX',
        'CSDB_INSTR_DESC_FULL_ODS',
        
        'CSDB_INSTR_RAT_FULL_ARCHIVE',
        'CSDB_INSTR_RAT_FULL_INBOX',
        'CSDB_INSTR_RAT_FULL_ODS',
        
        'CSDB_ISSUER_DESC_FULL_ARCHIVE',
        'CSDB_ISSUER_DESC_FULL_INBOX',
        'CSDB_ISSUER_DESC_FULL_ODS',
        
        'CSDB_ISSUER_RAT_FULL_ARCHIVE',
        'CSDB_ISSUER_RAT_FULL_INBOX',
        'CSDB_ISSUER_RAT_FULL_ODS'
    );
    
    vDroppedCount NUMBER := 0;
    vSkippedCount NUMBER := 0;
    vErrorCount NUMBER := 0;
BEGIN
    DBMS_OUTPUT.PUT_LINE('=========================================================================');
    DBMS_OUTPUT.PUT_LINE('MARS-1049-ADHOC: Drop CSDB External Tables');
    DBMS_OUTPUT.PUT_LINE('=========================================================================');
    DBMS_OUTPUT.PUT_LINE('Tables to process: ' || vTableList.COUNT);
    DBMS_OUTPUT.PUT_LINE('');
    
    -- Loop through table list
    FOR i IN 1..vTableList.COUNT LOOP
        BEGIN
            EXECUTE IMMEDIATE 'DROP TABLE ODS.' || vTableList(i);
            DBMS_OUTPUT.PUT_LINE('SUCCESS: Dropped ODS.' || vTableList(i));
            vDroppedCount := vDroppedCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -942 THEN
                    DBMS_OUTPUT.PUT_LINE('INFO: Table ODS.' || vTableList(i) || ' does not exist - skipping');
                    vSkippedCount := vSkippedCount + 1;
                ELSE
                    DBMS_OUTPUT.PUT_LINE('ERROR: Failed to drop ODS.' || vTableList(i) || ' - ' || SQLERRM);
                    vErrorCount := vErrorCount + 1;
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('=========================================================================');
    DBMS_OUTPUT.PUT_LINE('Drop External Tables - COMPLETED');
    DBMS_OUTPUT.PUT_LINE('=========================================================================');
    DBMS_OUTPUT.PUT_LINE('Summary:');
    DBMS_OUTPUT.PUT_LINE('- Tables processed: ' || vTableList.COUNT);
    DBMS_OUTPUT.PUT_LINE('- Tables dropped:   ' || vDroppedCount);
    DBMS_OUTPUT.PUT_LINE('- Tables skipped:   ' || vSkippedCount);
    DBMS_OUTPUT.PUT_LINE('- Errors:           ' || vErrorCount);
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('Next Step: Run 02_MARS_1049_ADHOC_recreate_external_tables.sql');
    DBMS_OUTPUT.PUT_LINE('=========================================================================');
END;
/
