-- ===================================================================
-- MARS-1005 Rollback Step 1: Delete File Registrations
-- ===================================================================
-- Purpose: Remove MARS-1005 export file registrations from A_SOURCE_FILE_RECEIVED
-- Author: Grzegorz Michalski
-- Date: 2026-02-12

SET SERVEROUTPUT ON SIZE UNLIMITED
SET TIMING ON

PROMPT =========================================================================
PROMPT MARS-1005 Rollback Step 1: Delete File Registrations
PROMPT =========================================================================

DECLARE
    vFileCount NUMBER := 0;
    vDeletedCount NUMBER := 0;
    vErrorMsg VARCHAR2(4000);
BEGIN
    -- Count files to be deleted (using PROCESS_NAME)
    SELECT COUNT(*)
    INTO vFileCount
    FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
    WHERE PROCESS_NAME = 'MARS-1005';
    
    DBMS_OUTPUT.PUT_LINE('Files to be deleted: ' || vFileCount);
    DBMS_OUTPUT.PUT_LINE('Using PROCESS_NAME = ''MARS-1005'' filter');
    
    IF vFileCount > 0 THEN
        -- Show files before deletion
        DBMS_OUTPUT.PUT_LINE('Files being removed:');
        FOR rec IN (
            SELECT A_SOURCE_FILE_RECEIVED_KEY, 
                   SUBSTR(SOURCE_FILE_NAME, 1, 60) AS FILE_NAME,
                   TO_CHAR(RECEPTION_DATE, 'YYYY-MM-DD HH24:MI:SS') AS RECEIVED_TIME,
                   PROCESS_NAME
            FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
            WHERE PROCESS_NAME = 'MARS-1005'
            ORDER BY RECEPTION_DATE DESC
        ) LOOP
            DBMS_OUTPUT.PUT_LINE('- ' || rec.FILE_NAME || ' (ID: ' || rec.A_SOURCE_FILE_RECEIVED_KEY || ', Process: ' || rec.PROCESS_NAME || ')');
        END LOOP;
        
        -- Delete the file registrations using PROCESS_NAME
        DELETE FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
        WHERE PROCESS_NAME = 'MARS-1005';
        
        vDeletedCount := SQL%ROWCOUNT;
        COMMIT;
        
        DBMS_OUTPUT.PUT_LINE('SUCCESS: Successfully deleted ' || vDeletedCount || ' file registrations');
        
        -- Log the rollback action
        INSERT INTO CT_MRDS.A_PROCESS_LOG (PROCESS_NAME, PROCEDURE_NAME, LOG_LEVEL, LOG_MESSAGE)
        VALUES ('MARS-1005-ROLLBACK', 'DELETE_FILE_REGISTRATIONS', 'INFO', 
                'Deleted ' || vDeletedCount || ' file registrations');
        COMMIT;
        
    ELSE
        DBMS_OUTPUT.PUT_LINE('SUCCESS: No file registrations found to delete');
    END IF;

EXCEPTION
    WHEN OTHERS THEN
        ROLLBACK;
        vErrorMsg := 'Failed to delete file registrations: ' || SQLERRM;
        DBMS_OUTPUT.PUT_LINE('ERROR: Error during file registration deletion: ' || SQLERRM);
        -- Log the error
        INSERT INTO CT_MRDS.A_PROCESS_LOG (PROCESS_NAME, PROCEDURE_NAME, LOG_LEVEL, LOG_MESSAGE)
        VALUES ('MARS-1005-ROLLBACK', 'DELETE_FILE_REGISTRATIONS', 'ERROR', vErrorMsg);
        COMMIT;
        RAISE;
END;
/

PROMPT =========================================================================
PROMPT File Registration Rollback Completed
PROMPT =========================================================================