-- ============================================================================
-- MARS-1409 Package Version Tracking
-- ============================================================================
-- Purpose: Record package versions in A_PACKAGE_VERSION_HISTORY table
-- ============================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT
PROMPT ============================================================================
PROMPT Recording Package Versions
PROMPT ============================================================================

DECLARE
    v_file_manager_version VARCHAR2(50);
    v_file_manager_build   VARCHAR2(100);
    v_env_manager_version  VARCHAR2(50);
    v_env_manager_build    VARCHAR2(100);
BEGIN
    -- Get FILE_MANAGER version
    BEGIN
        v_file_manager_version := CT_MRDS.FILE_MANAGER.GET_VERSION();
        v_file_manager_build   := CT_MRDS.FILE_MANAGER.GET_BUILD_INFO();
        
        DBMS_OUTPUT.PUT_LINE('FILE_MANAGER Version: ' || v_file_manager_version);
        DBMS_OUTPUT.PUT_LINE('FILE_MANAGER Build: ' || v_file_manager_build);
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: Could not retrieve FILE_MANAGER version');
    END;
    
    -- Get ENV_MANAGER version
    BEGIN
        v_env_manager_version := CT_MRDS.ENV_MANAGER.GET_VERSION();
        v_env_manager_build   := CT_MRDS.ENV_MANAGER.GET_BUILD_INFO();
        
        DBMS_OUTPUT.PUT_LINE('ENV_MANAGER Version: ' || v_env_manager_version);
        DBMS_OUTPUT.PUT_LINE('ENV_MANAGER Build: ' || v_env_manager_build);
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: Could not retrieve ENV_MANAGER version');
    END;
    
    -- Insert version records (if A_PACKAGE_VERSION_HISTORY table exists)
    BEGIN
        EXECUTE IMMEDIATE 'INSERT INTO CT_MRDS.A_PACKAGE_VERSION_HISTORY 
            (PACKAGE_NAME, PACKAGE_VERSION, BUILD_INFO, DEPLOYMENT_DATE, DEPLOYMENT_USER, MARS_TICKET)
            VALUES (:1, :2, :3, SYSDATE, USER, :4)'
        USING 'FILE_MANAGER', v_file_manager_version, v_file_manager_build, 'MARS-1409';
        
        EXECUTE IMMEDIATE 'INSERT INTO CT_MRDS.A_PACKAGE_VERSION_HISTORY 
            (PACKAGE_NAME, PACKAGE_VERSION, BUILD_INFO, DEPLOYMENT_DATE, DEPLOYMENT_USER, MARS_TICKET)
            VALUES (:1, :2, :3, SYSDATE, USER, :4)'
        USING 'ENV_MANAGER', v_env_manager_version, v_env_manager_build, 'MARS-1409';
        
        COMMIT;
        DBMS_OUTPUT.PUT_LINE('Package version history recorded successfully');
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('INFO: A_PACKAGE_VERSION_HISTORY table not available - skipping version tracking');
    END;
END;
/

PROMPT
PROMPT ============================================================================
PROMPT Version Tracking Complete
PROMPT ============================================================================
