--=============================================================================================================================
-- MARS-1005 ROLLBACK: Delete Exported CSV Files from DATA Bucket
--=============================================================================================================================
-- Purpose: Delete exported CSV files from ODS/TOP/ bucket folders for 6 OU_TOP LEGACY tables
-- WARNING: This will permanently delete exported data files!
-- Author: Grzegorz Michalski
-- Date: 2026-03-06
-- Related: MARS-1005 - OU_TOP Historical Data Export Rollback
--=============================================================================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting OU_TOP LEGACY exported files from DATA Bucket
PROMPT ========================================================================
PROMPT WARNING: This will delete files registered with PROCESS_NAME = 'MARS-1005'
PROMPT from "ODS/TOP/*" paths in the DATA bucket.
PROMPT ========================================================================

-- Helper: generic delete procedure for one TOP table folder
-- Deletes objects whose SOURCE_FILE_NAME matches the folder prefix pattern

-- ROLLBACK TABLE 1/6: LEGACY_ALLOTMENT -> ODS/TOP/TOP_ALLOTMENT/
PROMPT ROLLBACK: Deleting TOP_ALLOTMENT files...
DECLARE
    vDataBucketUri VARCHAR2(500);
    vFolderPath    VARCHAR2(200) := 'ODS/TOP/TOP_ALLOTMENT/';
    vFileCount     NUMBER := 0;
BEGIN
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    DBMS_OUTPUT.PUT_LINE('Deleting TOP_ALLOTMENT files registered by MARS-1005...');

    -- Only delete files registered by MARS-1005 (safe - does not touch pre-existing files)
    FOR rec IN (
        SELECT SOURCE_FILE_NAME
        FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-1005'
          AND SOURCE_FILE_NAME LIKE '%LEGACY_ALLOTMENT_%'
          AND SOURCE_FILE_NAME NOT LIKE '%MODIFICATION%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => 'OCI$RESOURCE_PRINCIPAL',
                object_uri      => vDataBucketUri || vFolderPath || rec.SOURCE_FILE_NAME
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.SOURCE_FILE_NAME);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.SOURCE_FILE_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: TOP_ALLOTMENT files deleted (' || vFileCount || ' file(s))');
END;
/

-- ROLLBACK TABLE 2/6: LEGACY_ALLOTMENT_MODIFICATION_HEADER -> ODS/TOP/TOP_ALLOTMENT_MODIFICATION_HEADER/
PROMPT ROLLBACK: Deleting TOP_ALLOTMENT_MODIFICATION_HEADER files...
DECLARE
    vDataBucketUri VARCHAR2(500);
    vFolderPath    VARCHAR2(200) := 'ODS/TOP/TOP_ALLOTMENT_MODIFICATION_HEADER/';
    vFileCount     NUMBER := 0;
BEGIN
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    DBMS_OUTPUT.PUT_LINE('Deleting TOP_ALLOTMENT_MODIFICATION_HEADER files registered by MARS-1005...');
    FOR rec IN (
        SELECT SOURCE_FILE_NAME FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-1005'
          AND SOURCE_FILE_NAME LIKE '%LEGACY_ALLOTMENT_MODIFICATION_HEADER%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(credential_name => 'OCI$RESOURCE_PRINCIPAL', object_uri => vDataBucketUri || vFolderPath || rec.SOURCE_FILE_NAME);
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.SOURCE_FILE_NAME);
            vFileCount := vFileCount + 1;
        EXCEPTION WHEN OTHERS THEN
            IF SQLCODE = -20404 THEN DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.SOURCE_FILE_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: TOP_ALLOTMENT_MODIFICATION_HEADER files deleted (' || vFileCount || ' file(s))');
END;
/

-- ROLLBACK TABLE 3/6: LEGACY_ALLOTMENT_MODIFICATION_ITEM -> ODS/TOP/TOP_ALLOTMENT_MODIFICATION_ITEM/
PROMPT ROLLBACK: Deleting TOP_ALLOTMENT_MODIFICATION_ITEM files...
DECLARE
    vDataBucketUri VARCHAR2(500);
    vFolderPath    VARCHAR2(200) := 'ODS/TOP/TOP_ALLOTMENT_MODIFICATION_ITEM/';
    vFileCount     NUMBER := 0;
BEGIN
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    DBMS_OUTPUT.PUT_LINE('Deleting TOP_ALLOTMENT_MODIFICATION_ITEM files registered by MARS-1005...');
    FOR rec IN (
        SELECT SOURCE_FILE_NAME FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-1005'
          AND SOURCE_FILE_NAME LIKE '%LEGACY_ALLOTMENT_MODIFICATION_ITEM%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(credential_name => 'OCI$RESOURCE_PRINCIPAL', object_uri => vDataBucketUri || vFolderPath || rec.SOURCE_FILE_NAME);
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.SOURCE_FILE_NAME);
            vFileCount := vFileCount + 1;
        EXCEPTION WHEN OTHERS THEN
            IF SQLCODE = -20404 THEN DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.SOURCE_FILE_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: TOP_ALLOTMENT_MODIFICATION_ITEM files deleted (' || vFileCount || ' file(s))');
END;
/

-- ROLLBACK TABLE 4/6: LEGACY_ANNOUNCEMENT -> ODS/TOP/TOP_ANNOUNCEMENT/
PROMPT ROLLBACK: Deleting TOP_ANNOUNCEMENT files...
DECLARE
    vDataBucketUri VARCHAR2(500);
    vFolderPath    VARCHAR2(200) := 'ODS/TOP/TOP_ANNOUNCEMENT/';
    vFileCount     NUMBER := 0;
BEGIN
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    DBMS_OUTPUT.PUT_LINE('Deleting TOP_ANNOUNCEMENT files registered by MARS-1005...');
    FOR rec IN (
        SELECT SOURCE_FILE_NAME FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-1005'
          AND SOURCE_FILE_NAME LIKE '%LEGACY_ANNOUNCEMENT%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(credential_name => 'OCI$RESOURCE_PRINCIPAL', object_uri => vDataBucketUri || vFolderPath || rec.SOURCE_FILE_NAME);
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.SOURCE_FILE_NAME);
            vFileCount := vFileCount + 1;
        EXCEPTION WHEN OTHERS THEN
            IF SQLCODE = -20404 THEN DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.SOURCE_FILE_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: TOP_ANNOUNCEMENT files deleted (' || vFileCount || ' file(s))');
END;
/

-- ROLLBACK TABLE 5/6: LEGACY_FBL_ITEM -> ODS/TOP/TOP_FULLBIDLIST_ITEM/
PROMPT ROLLBACK: Deleting TOP_FULLBIDLIST_ITEM files...
DECLARE
    vDataBucketUri VARCHAR2(500);
    vFolderPath    VARCHAR2(200) := 'ODS/TOP/TOP_FULLBIDLIST_ITEM/';
    vFileCount     NUMBER := 0;
BEGIN
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    DBMS_OUTPUT.PUT_LINE('Deleting TOP_FULLBIDLIST_ITEM files registered by MARS-1005...');
    FOR rec IN (
        SELECT SOURCE_FILE_NAME FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-1005'
          AND SOURCE_FILE_NAME LIKE '%LEGACY_FBL_ITEM%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(credential_name => 'OCI$RESOURCE_PRINCIPAL', object_uri => vDataBucketUri || vFolderPath || rec.SOURCE_FILE_NAME);
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.SOURCE_FILE_NAME);
            vFileCount := vFileCount + 1;
        EXCEPTION WHEN OTHERS THEN
            IF SQLCODE = -20404 THEN DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.SOURCE_FILE_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: TOP_FULLBIDLIST_ITEM files deleted (' || vFileCount || ' file(s))');
END;
/

-- ROLLBACK TABLE 6/6: LEGACY_FULLBID_ARRAY_COMPILED -> ODS/TOP/TOP_FULLBID_ARRAY_COMPILED/
PROMPT ROLLBACK: Deleting TOP_FULLBID_ARRAY_COMPILED files...
DECLARE
    vDataBucketUri VARCHAR2(500);
    vFolderPath    VARCHAR2(200) := 'ODS/TOP/TOP_FULLBID_ARRAY_COMPILED/';
    vFileCount     NUMBER := 0;
BEGIN
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    DBMS_OUTPUT.PUT_LINE('Deleting TOP_FULLBID_ARRAY_COMPILED files registered by MARS-1005...');
    FOR rec IN (
        SELECT SOURCE_FILE_NAME FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-1005'
          AND SOURCE_FILE_NAME LIKE '%LEGACY_FULLBID_ARRAY_COMPILED%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(credential_name => 'OCI$RESOURCE_PRINCIPAL', object_uri => vDataBucketUri || vFolderPath || rec.SOURCE_FILE_NAME);
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.SOURCE_FILE_NAME);
            vFileCount := vFileCount + 1;
        EXCEPTION WHEN OTHERS THEN
            IF SQLCODE = -20404 THEN DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.SOURCE_FILE_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: TOP_FULLBID_ARRAY_COMPILED files deleted (' || vFileCount || ' file(s))');
END;
/

PROMPT SUCCESS: All CSV file deletion operations completed
