-- ===================================================================
-- MARS-956 MASTER INSTALLATION SCRIPT
-- ===================================================================
-- Purpose: Export Historical C2D MPEC data from OU_C2D to DATA bucket
-- Author: Grzegorz Michalski
-- Date: 2026-02-11
-- 
-- Requirements:
-- - ADMIN user access for MARS installation
-- - OU_C2D schema access for source tables
-- - DATA_EXPORTER package v2.7.4+ deployed
-- - DEF_CRED_ARN credentials configured
-- - DATA bucket accessible
-- ===================================================================

-- Dynamic spool file generation
host mkdir log 2>nul
define spoolfile = 'log\install_mars956_'
define timestamp = ''

-- Get current timestamp for unique log filename
column current_time new_value timestamp
SELECT TO_CHAR(SYSDATE, 'YYYYMMDD_HH24MISS') AS current_time FROM dual;

-- Start logging
spool &spoolfile.&timestamp..log

-- Display environment information
PROMPT =========================================================================
PROMPT MARS-956 INSTALLATION - Export Historical C2D MPEC Data
PROMPT =========================================================================
PROMPT Installation Start: 
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS INSTALL_START FROM DUAL;

PROMPT Current User: 
SELECT USER AS CURRENT_USER FROM DUAL;

PROMPT Database Info:
SELECT INSTANCE_NAME, VERSION, STATUS FROM V$INSTANCE;

PROMPT =========================================================================
PROMPT Installation Details:
PROMPT - Purpose: One-time export of historical C2D MPEC delta data  
PROMPT - Source: OU_C2D schema tables (operational database)
PROMPT - Target: DATA bucket as CSV files
PROMPT - Tables: MPEC_ADMIN, MPEC_CONTENT, MPEC_CONTENT_CRITERION
PROMPT - Method: DATA_EXPORTER.EXPORT_TABLE_DATA_BY_DATE
PROMPT =========================================================================

SET SERVEROUTPUT ON SIZE 1000000
SET LINESIZE 200
SET PAGESIZE 1000

PROMPT
PROMPT Step 1: Verify Prerequisites
PROMPT =========================================================================

-- Verify DATA_EXPORTER package is available
PROMPT Checking DATA_EXPORTER package availability...
SELECT 'DATA_EXPORTER v' || CT_MRDS.DATA_EXPORTER.PACKAGE_VERSION || 
       ' (Build: ' || CT_MRDS.DATA_EXPORTER.PACKAGE_BUILD_DATE || ')' AS PACKAGE_INFO
FROM DUAL;

-- Verify source tables exist in OU_C2D
PROMPT Checking source tables in OU_C2D schema...
SELECT table_name, num_rows 
FROM all_tables 
WHERE owner = 'OU_C2D' 
  AND table_name IN ('MPEC_ADMIN', 'MPEC_CONTENT', 'MPEC_CONTENT_CRITERION')
ORDER BY table_name;

-- Verify template tables exist in CT_ET_TEMPLATES
PROMPT Checking template tables in CT_ET_TEMPLATES schema...
SELECT table_name
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 Step 2: Execute Historical Data Export
PROMPT =========================================================================
@@01_MARS_956_export_c2d_mpec_data.sql

PROMPT
PROMPT Step 3: Track Package Versions
PROMPT =========================================================================
@@track_package_versions.sql

PROMPT
PROMPT Step 4: Verify Package Versions  
PROMPT =========================================================================
@@verify_packages_version.sql

PROMPT
PROMPT =========================================================================
PROMPT MARS-956 INSTALLATION SUMMARY
PROMPT =========================================================================

-- Display final summary
PROMPT Installation Completed:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS INSTALL_END FROM DUAL;

PROMPT Export Results Summary:
SELECT COUNT(*) AS EXPORT_LOG_ENTRIES,
       MIN(EVENT_TIMESTAMP) AS FIRST_EXPORT,
       MAX(EVENT_TIMESTAMP) AS LAST_EXPORT
FROM CT_MRDS.A_PROCESS_LOG
WHERE PACKAGE_NAME = 'MARS-956'
  AND EVENT_TIMESTAMP >= SYSDATE - 1;  -- Last 24 hours

PROMPT
PROMPT =========================================================================
PROMPT POST-INSTALLATION TASKS
PROMPT =========================================================================
PROMPT 1. Verify CSV files created in DATA bucket:
PROMPT    - mrds_data_dev/ODS/C2D/C2D_MPEC_ADMIN/*.csv
PROMPT    - mrds_data_dev/ODS/C2D/C2D_MPEC_CONTENT/*.csv  
PROMPT    - mrds_data_dev/ODS/C2D/C2D_MPEC_CONTENT_CRITERION/*.csv
PROMPT
PROMPT 2. Check file structure matches template tables
PROMPT 3. Validate row counts match source tables
PROMPT 4. Confirm data available for delta queries
PROMPT 5. Sync deployment timing with REL_02 deployment
PROMPT =========================================================================

spool off
quit;