-- ===================================================================
-- MARS-1005 Rollback Verification: Confirm Rollback Completion
-- ===================================================================
-- Purpose: Verify that MARS-1005 rollback completed successfully
-- Author: Grzegorz Michalski
-- Date: 2026-02-12

SET SERVEROUTPUT ON SIZE UNLIMITED
SET TIMING ON

PROMPT =========================================================================
PROMPT MARS-1005 Rollback Verification
PROMPT =========================================================================

-- Check 1: Verify file registrations were removed
PROMPT Checking file registration cleanup...
DECLARE
    vRemainingFiles NUMBER := 0;
BEGIN
    SELECT COUNT(*)
    INTO vRemainingFiles
    FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
    WHERE SOURCE_FILE_NAME LIKE '200%'  -- ETL keys 2001-2010
      AND RECEPTION_DATE >= SYSDATE - 7;  -- Last week
    
    IF vRemainingFiles = 0 THEN
        DBMS_OUTPUT.PUT_LINE('SUCCESS: All MARS-1005 file registrations successfully removed');
    ELSE
        DBMS_OUTPUT.PUT_LINE('WARNING: ' || vRemainingFiles || ' file registrations still exist');
        
        -- Show remaining files
        FOR rec IN (
            SELECT SUBSTR(SOURCE_FILE_NAME, 1, 50) AS FILE_NAME,
                   TO_CHAR(RECEPTION_DATE, 'YYYY-MM-DD HH24:MI:SS') AS RECEIVED_TIME
            FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
            WHERE SOURCE_FILE_NAME LIKE '200%'
              AND RECEPTION_DATE >= SYSDATE - 7
        ) LOOP
            DBMS_OUTPUT.PUT_LINE('  Remaining: ' || rec.FILE_NAME);
        END LOOP;
    END IF;
END;
/

-- Check 2: Verify process logs were cleaned
PROMPT Checking process log cleanup...
DECLARE
    vRemainingLogs NUMBER := 0;
BEGIN
    SELECT COUNT(*)
    INTO vRemainingLogs
    FROM CT_MRDS.A_PROCESS_LOG
    WHERE PROCESS_NAME = 'MARS-1005'
      AND LOG_TIMESTAMP >= SYSDATE - 7;  -- Last week
    
    IF vRemainingLogs = 0 THEN
        DBMS_OUTPUT.PUT_LINE('SUCCESS: All MARS-1005 process logs successfully removed');
    ELSE
        DBMS_OUTPUT.PUT_LINE('WARNING: ' || vRemainingLogs || ' process log entries still exist');
        
        -- Show remaining logs (first few)
        FOR rec IN (
            SELECT TO_CHAR(LOG_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS LOG_TIME,
                   PROCEDURE_NAME,
                   SUBSTR(LOG_MESSAGE, 1, 40) AS MESSAGE
            FROM CT_MRDS.A_PROCESS_LOG
            WHERE PROCESS_NAME = 'MARS-1005'
              AND LOG_TIMESTAMP >= SYSDATE - 7
            ORDER BY LOG_TIMESTAMP DESC
            FETCH FIRST 3 ROWS ONLY
        ) LOOP
            DBMS_OUTPUT.PUT_LINE('  Remaining: ' || rec.LOG_TIME || ' ' || rec.PROCEDURE_NAME);
        END LOOP;
    END IF;
END;
/

-- Check 3: Verify cloud bucket cleanup (informational only)
PROMPT Checking cloud bucket status...
DECLARE
    vCloudFileCount NUMBER := 0;
    vCredentialName VARCHAR2(100);
    vDataBucketUri VARCHAR2(500);
BEGIN
    -- Get bucket URI and credential
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ODS');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('Checking ODS bucket: ' || vDataBucketUri);
    
    -- Count remaining files in cloud bucket
    BEGIN
        FOR rec IN (
            SELECT object_name
            FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
                credential_name => vCredentialName,
                location_uri => vDataBucketUri
            ))
            WHERE object_name LIKE 'ODS/C2D/C2D_MPEC_%'
        ) LOOP
            vCloudFileCount := vCloudFileCount + 1;
            IF vCloudFileCount <= 3 THEN  -- Show first 3 files
                DBMS_OUTPUT.PUT_LINE('  Cloud file: ' || rec.object_name);
            END IF;
        END LOOP;
        
        IF vCloudFileCount = 0 THEN
            DBMS_OUTPUT.PUT_LINE('SUCCESS: No C2D MPEC files found in cloud bucket');
        ELSE
            DBMS_OUTPUT.PUT_LINE('INFO: ' || vCloudFileCount || ' C2D MPEC files still in cloud bucket');
            DBMS_OUTPUT.PUT_LINE('  Note: Cloud files are not automatically deleted by rollback');
            DBMS_OUTPUT.PUT_LINE('  Manual deletion required if needed');
        END IF;
        
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: Cannot check cloud bucket: ' || SQLERRM);
    END;
END;
/

-- Check 4: Verify rollback logs were created
PROMPT Checking rollback operation logs...
DECLARE
    vRollbackLogs NUMBER := 0;
BEGIN
    SELECT COUNT(*)
    INTO vRollbackLogs
    FROM CT_MRDS.A_PROCESS_LOG
    WHERE PROCESS_NAME = 'MARS-1005-ROLLBACK'
      AND LOG_TIMESTAMP >= SYSDATE - 1/24;  -- Last hour
    
    IF vRollbackLogs > 0 THEN
        DBMS_OUTPUT.PUT_LINE('SUCCESS: Rollback operation logs found: ' || vRollbackLogs);
        
        -- Show recent rollback logs
        FOR rec IN (
            SELECT TO_CHAR(LOG_TIMESTAMP, 'HH24:MI:SS') AS LOG_TIME,
                   PROCEDURE_NAME,
                   LOG_LEVEL,
                   SUBSTR(LOG_MESSAGE, 1, 50) AS MESSAGE
            FROM CT_MRDS.A_PROCESS_LOG
            WHERE PROCESS_NAME = 'MARS-1005-ROLLBACK'
              AND LOG_TIMESTAMP >= SYSDATE - 1/24
            ORDER BY LOG_TIMESTAMP DESC
        ) LOOP
            DBMS_OUTPUT.PUT_LINE('  ' || rec.LOG_TIME || ' [' || rec.LOG_LEVEL || '] ' || 
                               rec.PROCEDURE_NAME || ': ' || rec.MESSAGE);
        END LOOP;
    ELSE
        DBMS_OUTPUT.PUT_LINE('WARNING: Warning: No rollback operation logs found');
    END IF;
END;
/

PROMPT 
PROMPT =========================================================================
PROMPT MARS-1005 Rollback Verification Summary
PROMPT =========================================================================

DECLARE
    vRemainingFiles NUMBER := 0;
    vRemainingLogs NUMBER := 0;
    vRollbackStatus VARCHAR2(20);
BEGIN
    -- Count remaining registrations
    SELECT COUNT(*)
    INTO vRemainingFiles
    FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
    WHERE SOURCE_FILE_NAME LIKE '200%'
      AND RECEPTION_DATE >= SYSDATE - 7;
    
    -- Count remaining process logs
    SELECT COUNT(*)
    INTO vRemainingLogs
    FROM CT_MRDS.A_PROCESS_LOG
    WHERE PROCESS_NAME = 'MARS-1005'
      AND LOG_TIMESTAMP >= SYSDATE - 7;
    
    -- Determine rollback status
    IF vRemainingFiles = 0 AND vRemainingLogs = 0 THEN
        vRollbackStatus := 'COMPLETE';
    ELSIF vRemainingFiles = 0 OR vRemainingLogs = 0 THEN
        vRollbackStatus := 'PARTIAL';
    ELSE
        vRollbackStatus := 'INCOMPLETE';
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('MARS-1005 Rollback Status: ' || vRollbackStatus);
    DBMS_OUTPUT.PUT_LINE('- Remaining file registrations: ' || vRemainingFiles);
    DBMS_OUTPUT.PUT_LINE('- Remaining process logs: ' || vRemainingLogs);
    
    IF vRollbackStatus = 'COMPLETE' THEN
        DBMS_OUTPUT.PUT_LINE('SUCCESS: Rollback completed successfully - system clean');
    ELSE
        DBMS_OUTPUT.PUT_LINE('WARNING: Rollback incomplete - manual cleanup may be required');
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('Note: Cloud bucket files (OCI) are not automatically removed');
    DBMS_OUTPUT.PUT_LINE('      Use OCI console or DBMS_CLOUD commands for file deletion if needed');
END;
/

PROMPT =========================================================================
PROMPT Rollback Verification Completed
PROMPT =========================================================================