-- MARS-828: Verify installation
-- Author: Grzegorz Michalski
-- Date: 2026-01-27

PROMPT ========================================
PROMPT MARS-828: Verification Script
PROMPT ========================================

SET SERVEROUTPUT ON SIZE UNLIMITED

-- 1. Verify columns added
PROMPT
PROMPT 1. Verifying A_SOURCE_FILE_CONFIG columns...
SELECT 
    column_name, 
    data_type, 
    nullable,
    data_default
FROM all_tab_columns 
WHERE owner = 'CT_MRDS' 
  AND table_name = 'A_SOURCE_FILE_CONFIG'
  AND column_name IN ('ARCHIVAL_STRATEGY', 'MINIMUM_AGE_MONTHS')
ORDER BY column_id;

-- 2. Verify constraint
PROMPT
PROMPT 2. Verifying check constraint...
SELECT 
    constraint_name,
    constraint_type,
    search_condition
FROM all_constraints 
WHERE owner = 'CT_MRDS' 
  AND table_name = 'A_SOURCE_FILE_CONFIG'
  AND constraint_name = 'CHK_ARCHIVAL_STRATEGY';

-- 3. Verify trigger
PROMPT
PROMPT 3. Verifying validation trigger...
SELECT 
    trigger_name,
    status,
    trigger_type
FROM all_triggers 
WHERE owner = 'CT_MRDS' 
  AND trigger_name = 'TRG_BI_A_SRC_FILE_CFG_ARCH_VAL';

-- 4. Check package compilation status
PROMPT
PROMPT 4. Checking FILE_ARCHIVER package status...
SELECT 
    object_name,
    object_type,
    status,
    TO_CHAR(last_ddl_time, 'YYYY-MM-DD HH24:MI:SS') as 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;

-- 5. Check FILE_MANAGER package compilation status
PROMPT
PROMPT 5. Checking FILE_MANAGER package status...
SELECT 
    object_name,
    object_type,
    status,
    TO_CHAR(last_ddl_time, 'YYYY-MM-DD HH24:MI:SS') as 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;

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

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

-- 8. Verify package versions
PROMPT
PROMPT 8. Verifying package versions...
PROMPT FILE_ARCHIVER version:
SELECT CT_MRDS.FILE_ARCHIVER.GET_VERSION() as package_version FROM DUAL;
PROMPT FILE_MANAGER version:
SELECT CT_MRDS.FILE_MANAGER.GET_VERSION() as package_version FROM DUAL;

-- 9. Test trigger validation
PROMPT
PROMPT 9. Testing trigger validation (should fail)...
WHENEVER SQLERROR CONTINUE
SET SERVEROUTPUT ON
DECLARE
    vTestPassed BOOLEAN := FALSE;
BEGIN
    -- This should fail - MINIMUM_AGE_MONTHS strategy without value
    INSERT INTO CT_MRDS.A_SOURCE_FILE_CONFIG (
        A_SOURCE_FILE_CONFIG_KEY,
        A_SOURCE_KEY,
        SOURCE_FILE_TYPE,
        SOURCE_FILE_ID,
        ARCHIVAL_STRATEGY,
        MINIMUM_AGE_MONTHS
    ) VALUES (
        -999,
        'TEST',
        'INPUT',
        'TEST',
        'MINIMUM_AGE_MONTHS',
        NULL  -- Should trigger error
    );
    
    ROLLBACK;
    DBMS_OUTPUT.PUT_LINE('ERROR: Trigger validation did not fire!');
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE = -20999 THEN
            DBMS_OUTPUT.PUT_LINE('SUCCESS: Trigger validation working correctly');
            DBMS_OUTPUT.PUT_LINE('Trigger correctly rejected MINIMUM_AGE_MONTHS strategy without required value');
            vTestPassed := TRUE;
        ELSE
            DBMS_OUTPUT.PUT_LINE('ERROR: Unexpected error occurred during trigger validation');
            DBMS_OUTPUT.PUT_LINE('Error code: ' || SQLCODE);
        END IF;
        ROLLBACK;
END;
/
WHENEVER SQLERROR EXIT FAILURE

PROMPT
PROMPT ========================================
PROMPT MARS-828: Verification Complete
PROMPT ========================================
