-- ============================================================================
-- MARS-1409 Rollback 90: Verify Rollback
-- ============================================================================
-- Purpose: Verify successful rollback of MARS-1409 changes
-- ============================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT
PROMPT ============================================================================
PROMPT Verifying MARS-1409 Rollback
PROMPT ============================================================================

-- Check if column was removed
PROMPT
PROMPT 1. Verifying A_WORKFLOW_HISTORY_KEY column removal...
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*)
    INTO v_count
    FROM all_tab_columns
    WHERE owner = 'CT_MRDS'
      AND table_name = 'A_SOURCE_FILE_RECEIVED'
      AND column_name = 'A_WORKFLOW_HISTORY_KEY';
    
    IF v_count = 0 THEN
        DBMS_OUTPUT.PUT_LINE('SUCCESS: A_WORKFLOW_HISTORY_KEY column removed');
    ELSE
        DBMS_OUTPUT.PUT_LINE('WARNING: A_WORKFLOW_HISTORY_KEY column still exists');
    END IF;
END;
/

-- Check trigger was restored
PROMPT
PROMPT 1B. Checking A_WORKFLOW_HISTORY trigger status...
DECLARE
    v_status VARCHAR2(20);
BEGIN
    SELECT status INTO v_status
    FROM all_objects
    WHERE owner = 'CT_MRDS'
      AND object_name = 'A_WORKFLOW_HISTORY'
      AND object_type = 'TRIGGER';
    DBMS_OUTPUT.PUT_LINE('A_WORKFLOW_HISTORY status: ' || v_status);
EXCEPTION
    WHEN NO_DATA_FOUND THEN
        DBMS_OUTPUT.PUT_LINE('WARNING: A_WORKFLOW_HISTORY not found');
END;
/

-- Check compilation status
PROMPT
PROMPT 2. Checking package compilation status...
SELECT 
    object_name,
    object_type,
    status,
    last_ddl_time
FROM all_objects
WHERE owner = 'CT_MRDS'
  AND object_name IN ('FILE_MANAGER', 'ENV_MANAGER', 'FILE_ARCHIVER', 'DATA_EXPORTER')
  AND object_type IN ('PACKAGE', 'PACKAGE BODY')
ORDER BY object_name, object_type;

-- Check for compilation errors
PROMPT
PROMPT 3. Checking for compilation errors...
SELECT name, type, line, position, text
FROM all_errors
WHERE owner = 'CT_MRDS'
  AND name IN ('FILE_MANAGER', 'ENV_MANAGER', 'FILE_ARCHIVER', 'DATA_EXPORTER')
ORDER BY name, type, line, position;

-- Verify package versions
PROMPT
PROMPT 4. Verifying package versions after rollback...
SELECT 'FILE_MANAGER'  AS PACKAGE_NAME, CT_MRDS.FILE_MANAGER.GET_VERSION()  AS VERSION FROM DUAL
UNION ALL
SELECT 'ENV_MANAGER'   AS PACKAGE_NAME, CT_MRDS.ENV_MANAGER.GET_VERSION()   AS VERSION FROM DUAL
UNION ALL
SELECT 'FILE_ARCHIVER' AS PACKAGE_NAME, CT_MRDS.FILE_ARCHIVER.GET_VERSION() AS VERSION FROM DUAL
UNION ALL
SELECT 'DATA_EXPORTER' AS PACKAGE_NAME, CT_MRDS.DATA_EXPORTER.GET_VERSION() AS VERSION FROM DUAL;

PROMPT
PROMPT ============================================================================
PROMPT Rollback Verification Complete
PROMPT ============================================================================
