--=============================================================================================================================
-- MARS-956 ROLLBACK: Delete Exported CSV Files from DATA Bucket
--=============================================================================================================================
-- Purpose: Delete exported CSV files from ODS/C2D bucket folders for MPEC tables
-- WARNING: This will permanently delete exported data files!
-- Author: Grzegorz Michalski
-- Date: 2026-02-12
-- Related: MARS-956 - C2D MPEC Data Export Rollback
--=============================================================================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting C2D_MPEC_ADMIN exported files
PROMPT ========================================================================
PROMPT WARNING: This will delete files from:
PROMPT   - DATA bucket: mrds_data_dev/ODS/C2D/C2D_MPEC_ADMIN/
PROMPT ========================================================================

DECLARE
    vDataBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
BEGIN
    -- Get bucket URI and credential
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('Deleting C2D_MPEC_ADMIN files from DATA bucket...');
    DBMS_OUTPUT.PUT_LINE('  Using A_SOURCE_FILE_RECEIVED with PROCESS_NAME = ''MARS-956''');
    
    -- Delete CSV files registered by MARS-956 process
    FOR rec IN (
        SELECT SOURCE_FILE_NAME AS object_name
        FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-956'
          AND SOURCE_FILE_NAME LIKE '%MPEC_ADMIN%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vDataBucketUri || 'ODS/C2D/C2D_MPEC_ADMIN/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    IF vFileCount = 0 THEN
        DBMS_OUTPUT.PUT_LINE('  INFO: No files found to delete');
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: C2D_MPEC_ADMIN files deleted (' || vFileCount || ' file(s))');
END;
/

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting C2D_MPEC_CONTENT exported files
PROMPT ========================================================================
PROMPT WARNING: This will delete files from:
PROMPT   - DATA bucket: mrds_data_dev/ODS/C2D/C2D_MPEC_CONTENT/
PROMPT ========================================================================

DECLARE
    vDataBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
BEGIN
    -- Get bucket URI and credential
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('Deleting C2D_MPEC_CONTENT files from DATA bucket...');
    DBMS_OUTPUT.PUT_LINE('  Using A_SOURCE_FILE_RECEIVED with PROCESS_NAME = ''MARS-956''');
    
    -- Delete CSV files registered by MARS-956 process
    FOR rec IN (
        SELECT SOURCE_FILE_NAME AS object_name
        FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-956'
          AND SOURCE_FILE_NAME LIKE '%MPEC_CONTENT%'
          AND SOURCE_FILE_NAME NOT LIKE '%CRITERION%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vDataBucketUri || 'ODS/C2D/C2D_MPEC_CONTENT/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    IF vFileCount = 0 THEN
        DBMS_OUTPUT.PUT_LINE('  INFO: No files found to delete');
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: C2D_MPEC_CONTENT files deleted (' || vFileCount || ' file(s))');
END;
/

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting C2D_MPEC_CONTENT_CRITERION exported files
PROMPT ========================================================================
PROMPT WARNING: This will delete files from:
PROMPT   - DATA bucket: mrds_data_dev/ODS/C2D/C2D_MPEC_CONTENT_CRITERION/
PROMPT ========================================================================

DECLARE
    vDataBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
BEGIN
    -- Get bucket URI and credential
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('Deleting C2D_MPEC_CONTENT_CRITERION files from DATA bucket...');
    DBMS_OUTPUT.PUT_LINE('  Using A_SOURCE_FILE_RECEIVED with PROCESS_NAME = ''MARS-956''');
    
    -- Delete CSV files registered by MARS-956 process
    FOR rec IN (
        SELECT SOURCE_FILE_NAME AS object_name
        FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-956'
          AND SOURCE_FILE_NAME LIKE '%MPEC_CONTENT_CRITERION%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vDataBucketUri || 'ODS/C2D/C2D_MPEC_CONTENT_CRITERION/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    IF vFileCount = 0 THEN
        DBMS_OUTPUT.PUT_LINE('  INFO: No files found to delete');
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: C2D_MPEC_CONTENT_CRITERION files deleted (' || vFileCount || ' file(s))');
END;
/

PROMPT SUCCESS: All CSV file deletion operations completed
