-- ===================================================================
-- MARS-956 POST-EXPORT VALIDATION SCRIPT
-- ===================================================================
-- Purpose: Validate C2D MPEC historical data export results
-- Author: Grzegorz Michalski
-- Date: 2026-02-11
-- 
-- Run after MARS-956 installation to verify export success
-- ===================================================================

SET LINESIZE 200
SET PAGESIZE 1000
SET SERVEROUTPUT ON SIZE 1000000

PROMPT =========================================================================
PROMPT MARS-956 POST-EXPORT VALIDATION
PROMPT =========================================================================
PROMPT Validation Start:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS VALIDATION_START FROM DUAL;

PROMPT
PROMPT 1. Export Process Log Review
PROMPT =========================================================================

-- Check export completion status
PROMPT Recent MARS-956 export activity:
SELECT TO_CHAR(EVENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS EVENT_TIME,
       PROCEDURE_NAME,
       EVENT_TYPE,
       SUBSTR(EVENT_MESSAGE, 1, 80) AS MESSAGE_PREVIEW
FROM CT_MRDS.A_PROCESS_LOG
WHERE PACKAGE_NAME = 'MARS-956'
   OR PROCEDURE_NAME LIKE '%MARS_956%'
   OR PROCEDURE_NAME LIKE '%DATA_EXPORTER%'
ORDER BY EVENT_TIMESTAMP DESC
FETCH FIRST 20 ROWS ONLY;

-- Check for any errors
PROMPT Export errors (if any):
SELECT TO_CHAR(EVENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS ERROR_TIME,
       PROCEDURE_NAME,
       EVENT_MESSAGE
FROM CT_MRDS.A_PROCESS_LOG
WHERE (PACKAGE_NAME = 'MARS-956' OR PROCEDURE_NAME LIKE '%MARS_956%')
  AND EVENT_TYPE = 'ERROR'
  AND EVENT_TIMESTAMP >= SYSDATE - 1;  -- Last 24 hours

PROMPT
PROMPT 2. Source Table Row Counts
PROMPT =========================================================================

-- Get source table counts for comparison
PROMPT Source table row counts (OU_C2D):
SELECT 'OU_C2D' AS SCHEMA_NAME,
       table_name,
       num_rows,
       TO_CHAR(last_analyzed, 'YYYY-MM-DD HH24:MI:SS') AS STATS_DATE
FROM all_tables
WHERE owner = 'OU_C2D'
  AND table_name IN ('MPEC_ADMIN', 'MPEC_CONTENT', 'MPEC_CONTENT_CRITERION')
ORDER BY table_name;

PROMPT
PROMPT 3. Template Table Structure Verification
PROMPT =========================================================================

-- Verify template tables exist and have proper structure
PROMPT Template tables in CT_ET_TEMPLATES:
SELECT table_name,
       num_rows,
       TO_CHAR(last_analyzed, 'YYYY-MM-DD HH24:MI:SS') AS STATS_DATE
FROM all_tables
WHERE owner = 'CT_ET_TEMPLATES'
  AND table_name IN ('C2D_MPEC_ADMIN', 'C2D_MPEC_CONTENT', 'C2D_MPEC_CONTENT_CRITERION')
ORDER BY table_name;

PROMPT
PROMPT Template table column counts:
SELECT owner, table_name, COUNT(*) AS COLUMN_COUNT
FROM all_tab_columns
WHERE owner IN ('OU_C2D', 'CT_ET_TEMPLATES')
  AND ((owner = 'OU_C2D' AND table_name IN ('MPEC_ADMIN', 'MPEC_CONTENT', 'MPEC_CONTENT_CRITERION'))
    OR (owner = 'CT_ET_TEMPLATES' AND table_name IN ('C2D_MPEC_ADMIN', 'C2D_MPEC_CONTENT', 'C2D_MPEC_CONTENT_CRITERION')))
GROUP BY owner, table_name
ORDER BY table_name, owner;

PROMPT
PROMPT 4. Export File Validation Commands
PROMPT =========================================================================

PROMPT To validate exported CSV files, use these OCI CLI commands:
PROMPT
PROMPT # List exported files
PROMPT oci os object list --bucket-name mrds_data_dev --prefix "ODS/C2D/C2D_MPEC"
PROMPT
PROMPT # Check file sizes
PROMPT oci os object list --bucket-name mrds_data_dev --prefix "ODS/C2D/C2D_MPEC_ADMIN"
PROMPT oci os object list --bucket-name mrds_data_dev --prefix "ODS/C2D/C2D_MPEC_CONTENT" 
PROMPT oci os object list --bucket-name mrds_data_dev --prefix "ODS/C2D/C2D_MPEC_CONTENT_CRITERION"
PROMPT
PROMPT # Download sample file for validation
PROMPT oci os object get --bucket-name mrds_data_dev --name "ODS/C2D/C2D_MPEC_ADMIN/filename.csv" --file sample.csv

PROMPT
PROMPT 5. Data Quality Checks (Manual)
PROMPT =========================================================================

PROMPT Manual verification steps:
PROMPT 1. Download sample CSV files from each folder
PROMPT 2. Verify CSV header matches template table columns
PROMPT 3. Check data formats (especially dates) match expectations
PROMPT 4. Confirm row counts approximately match source tables
PROMPT 5. Validate no empty files were created
PROMPT 6. Test loading sample data into external tables

PROMPT
PROMPT 6. Next Steps for ODS Integration
PROMPT =========================================================================

PROMPT After validation success:
PROMPT 1. Configure external tables pointing to CSV files
PROMPT 2. Test external table queries
PROMPT 3. Setup scheduled data refresh processes (if needed)
PROMPT 4. Document file locations and access patterns
PROMPT 5. Coordinate with REL_02 deployment timing

PROMPT
PROMPT =========================================================================
PROMPT VALIDATION COMPLETED
PROMPT =========================================================================
PROMPT Validation End:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS VALIDATION_END FROM DUAL;

PROMPT
PROMPT Review the output above to confirm:
PROMPT ✓ Export processes completed without errors
PROMPT ✓ Source table row counts are reasonable
PROMPT ✓ Template tables exist and have matching structure
PROMPT ✓ Manual file validation steps are understood
PROMPT
PROMPT If any issues found, check export logs and re-run specific exports if needed.
PROMPT =========================================================================