-- ============================================================================
-- MARS-1409 Step 04: Verify Installation
-- ============================================================================
-- Purpose: Verify successful installation of MARS-1409 changes
-- ============================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED
SET VERIFY OFF
SET FEEDBACK ON

PROMPT
PROMPT ============================================================================
PROMPT Verifying MARS-1409 Installation
PROMPT ============================================================================

-- Check if column was added
PROMPT
PROMPT 1. Checking A_WORKFLOW_HISTORY_KEY column existence...
SELECT 
    column_name, 
    data_type, 
    data_length,
    nullable
FROM all_tab_columns
WHERE owner = 'CT_MRDS'
  AND table_name = 'A_SOURCE_FILE_RECEIVED'
  AND column_name = 'A_WORKFLOW_HISTORY_KEY';

-- Check foreign key constraint
PROMPT
PROMPT 2. Checking foreign key constraint...
SELECT 
    constraint_name,
    constraint_type,
    r_constraint_name,
    status
FROM all_constraints
WHERE owner = 'CT_MRDS'
  AND table_name = 'A_SOURCE_FILE_RECEIVED'
  AND constraint_type = 'R'
  AND constraint_name LIKE '%WORKFLOW_HISTORY%';

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

-- Check for compilation errors
PROMPT
PROMPT 4. Checking for compilation errors...
SELECT 
    name,
    type,
    line,
    position,
    text
FROM all_errors
WHERE owner = 'CT_MRDS'
  AND name = 'FILE_MANAGER'
ORDER BY type, line, position;

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

SELECT 
    name,
    type,
    line,
    position,
    text
FROM all_errors
WHERE owner = 'CT_MRDS'
  AND name = 'FILE_ARCHIVER'
ORDER BY type, line, position;

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

SELECT 
    name,
    type,
    line,
    position,
    text
FROM all_errors
WHERE owner = 'CT_MRDS'
  AND name = 'DATA_EXPORTER'
ORDER BY type, line, position;

-- Check trigger status
PROMPT
PROMPT 5B. Checking A_WORKFLOW_HISTORY trigger...
SELECT 
    trigger_name,
    trigger_type,
    triggering_event,
    status
FROM all_triggers
WHERE owner = 'CT_MRDS'
  AND trigger_name = 'A_WORKFLOW_HISTORY';

-- Verify package versions
PROMPT
PROMPT 6. Verifying package versions...
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 Verification Complete
PROMPT ============================================================================
