-- ============================================================================
-- MARS-1409 Step 09: Update A_WORKFLOW_HISTORY_KEY for existing records
-- ============================================================================
-- Purpose: Populate A_WORKFLOW_HISTORY_KEY for existing A_SOURCE_FILE_RECEIVED records
--          by extracting values from corresponding ODS tables
-- Prerequisites: 
--   - A_WORKFLOW_HISTORY_KEY column exists in A_SOURCE_FILE_RECEIVED
--   - ODS tables contain A_WORKFLOW_HISTORY_KEY and file$name columns
-- ============================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT
PROMPT Updating A_WORKFLOW_HISTORY_KEY for existing A_SOURCE_FILE_RECEIVED records...

DECLARE
    vUpdatedTotal    NUMBER := 0;
    vUpdatedCurrent  NUMBER := 0;
    vFailedConfigs   NUMBER := 0;
    vSkippedConfigs  NUMBER := 0;
    vTableName       VARCHAR2(200);
    vSQL             VARCHAR2(4000);
    vRecordsToUpdate NUMBER := 0;
    
BEGIN
    -- Count total records to update
    SELECT COUNT(*) INTO vRecordsToUpdate
    FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
    WHERE A_WORKFLOW_HISTORY_KEY IS NULL
      AND PROCESSING_STATUS IN ('VALIDATED', 'READY_FOR_INGESTION', 'INGESTED', 'ARCHIVED', 'ARCHIVED_AND_TRASHED', 'ARCHIVED_AND_PURGED');
    
    DBMS_OUTPUT.PUT_LINE('Found ' || vRecordsToUpdate || ' records with NULL A_WORKFLOW_HISTORY_KEY');
    DBMS_OUTPUT.PUT_LINE('----------------------------------------');
    
    -- Process each INPUT configuration
    FOR config_rec IN (
        SELECT DISTINCT 
            sfc.A_SOURCE_FILE_CONFIG_KEY,
            sfc.A_SOURCE_KEY,
            sfc.SOURCE_FILE_ID,
            sfc.TABLE_ID,
            sfc.TEMPLATE_TABLE_NAME,
            (SELECT COUNT(*) 
             FROM CT_MRDS.A_SOURCE_FILE_RECEIVED sfr 
             WHERE sfr.A_SOURCE_FILE_CONFIG_KEY = sfc.A_SOURCE_FILE_CONFIG_KEY
               AND sfr.A_WORKFLOW_HISTORY_KEY IS NULL
               AND sfr.PROCESSING_STATUS IN ('VALIDATED', 'READY_FOR_INGESTION', 'INGESTED', 'ARCHIVED', 'ARCHIVED_AND_TRASHED', 'ARCHIVED_AND_PURGED')
            ) as NULL_COUNT
        FROM CT_MRDS.A_SOURCE_FILE_CONFIG sfc
        WHERE sfc.SOURCE_FILE_TYPE = 'INPUT'
          AND sfc.TABLE_ID IS NOT NULL
        ORDER BY sfc.A_SOURCE_KEY, sfc.SOURCE_FILE_ID, sfc.TABLE_ID
    ) LOOP
        
        IF config_rec.NULL_COUNT = 0 THEN
            vSkippedConfigs := vSkippedConfigs + 1;
            DBMS_OUTPUT.PUT_LINE('SKIP: Config ' || config_rec.A_SOURCE_FILE_CONFIG_KEY || 
                               ' (' || config_rec.A_SOURCE_KEY || '/' || config_rec.SOURCE_FILE_ID || '/' || config_rec.TABLE_ID || 
                               ') - no records to update');
            CONTINUE;
        END IF;
        
        BEGIN
            -- Construct ODS table name from TABLE_ID (ODS tables have _ODS suffix)
            vTableName := 'ODS.' || config_rec.TABLE_ID || '_ODS';
            
            DBMS_OUTPUT.PUT_LINE('Processing config ' || config_rec.A_SOURCE_FILE_CONFIG_KEY || 
                               ' (' || config_rec.A_SOURCE_KEY || '/' || config_rec.SOURCE_FILE_ID || '/' || config_rec.TABLE_ID || ')...');
            
            -- Try to update using ODS table
            -- Uses MIN to handle edge case of multiple workflow keys (shouldn't happen, but defensive)
            vSQL := 
                'UPDATE CT_MRDS.A_SOURCE_FILE_RECEIVED sfr ' ||
                'SET A_WORKFLOW_HISTORY_KEY = ( ' ||
                '    SELECT MIN(t.A_WORKFLOW_HISTORY_KEY) ' ||
                '    FROM ' || vTableName || ' t ' ||
                '    WHERE t.file$name = sfr.SOURCE_FILE_NAME ' ||
                '      AND t.A_WORKFLOW_HISTORY_KEY IS NOT NULL ' ||
                ') ' ||
                'WHERE sfr.A_SOURCE_FILE_CONFIG_KEY = :config_key ' ||
                '  AND sfr.A_WORKFLOW_HISTORY_KEY IS NULL ' ||
                '  AND sfr.PROCESSING_STATUS IN (''VALIDATED'', ''READY_FOR_INGESTION'', ''INGESTED'', ''ARCHIVED'', ''ARCHIVED_AND_TRASHED'', ''ARCHIVED_AND_PURGED'') ' ||
                '  AND EXISTS ( ' ||
                '      SELECT 1 FROM ' || vTableName || ' t ' ||
                '      WHERE t.file$name = sfr.SOURCE_FILE_NAME ' ||
                '        AND t.A_WORKFLOW_HISTORY_KEY IS NOT NULL ' ||
                '  )';
            
            EXECUTE IMMEDIATE vSQL USING config_rec.A_SOURCE_FILE_CONFIG_KEY;
            
            vUpdatedCurrent := SQL%ROWCOUNT;
            vUpdatedTotal := vUpdatedTotal + vUpdatedCurrent;
            
            IF vUpdatedCurrent > 0 THEN
                DBMS_OUTPUT.PUT_LINE('  SUCCESS: Updated ' || vUpdatedCurrent || ' record(s)');
            ELSE
                DBMS_OUTPUT.PUT_LINE('  INFO: No matching records found in ODS table (files may not be ingested yet)');
            END IF;
            
        EXCEPTION
            WHEN OTHERS THEN
                vFailedConfigs := vFailedConfigs + 1;
                DBMS_OUTPUT.PUT_LINE('  ERROR: Failed for config ' || config_rec.A_SOURCE_FILE_CONFIG_KEY || 
                                   ' (table: ' || vTableName || ')');
                DBMS_OUTPUT.PUT_LINE('  Reason: ' || SQLERRM);
                -- Continue processing other configurations despite this failure
        END;
    END LOOP;
    
    COMMIT;
    
    DBMS_OUTPUT.PUT_LINE('----------------------------------------');
    DBMS_OUTPUT.PUT_LINE('Update Summary:');
    DBMS_OUTPUT.PUT_LINE('  Total records updated: ' || vUpdatedTotal);
    DBMS_OUTPUT.PUT_LINE('  Configurations skipped (no NULL records): ' || vSkippedConfigs);
    DBMS_OUTPUT.PUT_LINE('  Configurations failed: ' || vFailedConfigs);
    
    -- Check remaining NULL records
    SELECT COUNT(*) INTO vRecordsToUpdate
    FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
    WHERE A_WORKFLOW_HISTORY_KEY IS NULL
    --   AND PROCESSING_STATUS IN ('VALIDATED', 'READY_FOR_INGESTION', 'INGESTED', 'ARCHIVED', 'ARCHIVED_AND_TRASHED', 'ARCHIVED_AND_PURGED')
      ;
    
    DBMS_OUTPUT.PUT_LINE('  Remaining NULL records: ' || vRecordsToUpdate);
    
    IF vRecordsToUpdate > 0 THEN
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('NOTE: Some records still have NULL A_WORKFLOW_HISTORY_KEY.');
        DBMS_OUTPUT.PUT_LINE('      This is expected for:');
        DBMS_OUTPUT.PUT_LINE('      - Files not yet ingested into ODS tables');
        DBMS_OUTPUT.PUT_LINE('      - Files with status RECEIVED or VALIDATION_FAILED');
        DBMS_OUTPUT.PUT_LINE('      - ODS tables that do not exist or have different structure');
        DBMS_OUTPUT.PUT_LINE('      These records will be populated when files are processed through VALIDATE_SOURCE_FILE_RECEIVED');
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('----------------------------------------');
    
EXCEPTION
    WHEN OTHERS THEN
        ROLLBACK;
        DBMS_OUTPUT.PUT_LINE('FATAL ERROR: ' || SQLERRM);
        DBMS_OUTPUT.PUT_LINE('Transaction rolled back');
        RAISE;
END;
/

PROMPT
PROMPT Existing workflow keys update completed!
PROMPT
