-- =====================================================
-- RUN ALL PHASE 2 MIGRATION SCRIPTS
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Execute all Phase 2 data migration scripts in sequence
-- Status: Ready for DEV testing
-- Estimated Time: 10-15 minutes
-- =====================================================
--
-- PREREQUISITES:
-- 1. Phase 1 must be complete (schema changes)
-- 2. Database backup created
-- 3. Verify Phase 1 completion:
--    SELECT COUNT(*) FROM comstypes;  -- Should have records
--    SELECT COUNT(*) FROM relationshiptypes;  -- Should have records
--    DESCRIBE machines;  -- Should show new columns
--
-- USAGE:
--   mysql -u root -p shopdb < RUN_ALL_PHASE2_SCRIPTS.sql | tee /tmp/phase2_migration.log
--
-- Or run interactively:
--   mysql -u root -p shopdb
--   source RUN_ALL_PHASE2_SCRIPTS.sql
--
-- =====================================================

USE shopdb;

SELECT '============================================================' AS '';
SELECT 'PHASE 2: PC DATA MIGRATION' AS '';
SELECT '============================================================' AS '';
SELECT 'Starting Phase 2 data migration...' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

-- Store start time
SET @phase2_start_time = NOW();

-- =====================================================
-- PRE-MIGRATION CHECKS
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'PRE-MIGRATION CHECKS' AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- Check Phase 1 completion
SELECT 'Verifying Phase 1 completion...' AS '';

SET @phase1_tables_count = (
    SELECT COUNT(*) FROM information_schema.tables
    WHERE table_schema = 'shopdb'
    AND table_name IN (
        'comstypes', 'communications', 'warranties', 'compliance',
        'relationshiptypes', 'machinerelationships', 'machinestatus'
    )
);

SELECT CASE
    WHEN @phase1_tables_count = 7 THEN '✓ Phase 1 tables found'
    ELSE '⚠️ ERROR: Phase 1 incomplete - run Phase 1 scripts first!'
END AS status;

-- Check for required columns in machines table
SET @machines_columns = (
    SELECT COUNT(*) FROM information_schema.columns
    WHERE table_schema = 'shopdb'
    AND table_name = 'machines'
    AND column_name IN ('hostname', 'osid', 'pctypeid', 'machinestatusid')
);

SELECT CASE
    WHEN @machines_columns = 4 THEN '✓ Machines table extended'
    ELSE '⚠️ ERROR: Machines table not extended - run Phase 1 scripts first!'
END AS status;

-- Check comstypes populated
SET @comstypes_count = (SELECT COUNT(*) FROM comstypes);

SELECT CASE
    WHEN @comstypes_count >= 3 THEN CONCAT('✓ Comstypes populated (', @comstypes_count, ' types)')
    ELSE '⚠️ ERROR: Comstypes not populated - run Phase 1 scripts first!'
END AS status;

-- Check relationshiptypes populated
SET @relationshiptypes_count = (SELECT COUNT(*) FROM relationshiptypes);

SELECT CASE
    WHEN @relationshiptypes_count >= 3 THEN CONCAT('✓ Relationship types populated (', @relationshiptypes_count, ' types)')
    ELSE '⚠️ ERROR: Relationship types not populated - run Phase 1 scripts first!'
END AS status;

SELECT '' AS '';

-- Show current state
SELECT 'Current state:' AS '';
SELECT
    'Active PCs to migrate' AS category,
    COUNT(*) as count
FROM pc WHERE isactive = 1
UNION ALL
SELECT
    'Current machines (before migration)',
    COUNT(*)
FROM machines WHERE isactive = 1
UNION ALL
SELECT
    'Network interfaces to migrate',
    COUNT(*)
FROM pc_network_interfaces WHERE isactive = 1
UNION ALL
SELECT
    'Comm configs to migrate',
    COUNT(*)
FROM pc_comm_config
UNION ALL
SELECT
    'PCs with warranty data',
    COUNT(*)
FROM pc WHERE warrantyenddate IS NOT NULL AND isactive = 1
UNION ALL
SELECT
    'Dualpath assignments to migrate',
    COUNT(*)
FROM pc_dualpath_assignments;

SELECT '' AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- Pause to allow review
SELECT 'Pre-migration checks complete. Proceeding with migration...' AS '';
SELECT '' AS '';

-- =====================================================
-- SCRIPT 01: Migrate PCs to Machines
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'SCRIPT 01: MIGRATE PCS TO MACHINES' AS '';
SELECT '============================================================' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

source 01_migrate_pcs_to_machines.sql

SELECT '' AS '';
SELECT 'Script 01 completed' AS '';
SELECT CONCAT('Time: ', NOW()) AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- =====================================================
-- SCRIPT 02: Migrate Network Interfaces
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'SCRIPT 02: MIGRATE NETWORK INTERFACES' AS '';
SELECT '============================================================' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

source 02_migrate_network_interfaces_to_communications.sql

SELECT '' AS '';
SELECT 'Script 02 completed' AS '';
SELECT CONCAT('Time: ', NOW()) AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- =====================================================
-- SCRIPT 03: Migrate Comm Config
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'SCRIPT 03: MIGRATE COMM CONFIG' AS '';
SELECT '============================================================' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

source 03_migrate_comm_config_to_communications.sql

SELECT '' AS '';
SELECT 'Script 03 completed' AS '';
SELECT CONCAT('Time: ', NOW()) AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- =====================================================
-- SCRIPT 04: Migrate Warranties
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'SCRIPT 04: MIGRATE WARRANTIES' AS '';
SELECT '============================================================' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

source 04_migrate_warranties.sql

SELECT '' AS '';
SELECT 'Script 04 completed' AS '';
SELECT CONCAT('Time: ', NOW()) AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- =====================================================
-- SCRIPT 05: Migrate Dualpath Assignments
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'SCRIPT 05: MIGRATE DUALPATH ASSIGNMENTS' AS '';
SELECT '============================================================' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

source 05_migrate_dualpath_assignments.sql

SELECT '' AS '';
SELECT 'Script 05 completed' AS '';
SELECT CONCAT('Time: ', NOW()) AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- =====================================================
-- VERIFICATION
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'RUNNING VERIFICATION' AS '';
SELECT '============================================================' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

source VERIFY_PHASE2_MIGRATION.sql

SELECT '' AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- =====================================================
-- FINAL SUMMARY
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'PHASE 2 MIGRATION COMPLETE' AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

-- Calculate elapsed time
SELECT CONCAT('Start time: ', @phase2_start_time) AS '';
SELECT CONCAT('End time: ', NOW()) AS '';
SELECT CONCAT('Elapsed time: ', TIMESTAMPDIFF(MINUTE, @phase2_start_time, NOW()), ' minutes') AS '';

SELECT '' AS '';

-- Final counts
SELECT 'Final migration statistics:' AS '';
SELECT
    'PCs migrated to machines' AS category,
    COUNT(*) as count
FROM machines WHERE pctypeid IS NOT NULL
UNION ALL
SELECT
    'Network interfaces migrated',
    COUNT(*)
FROM communications c
JOIN comstypes ct ON c.comstypeid = ct.comstypeid
WHERE ct.typename = 'Network_Interface'
UNION ALL
SELECT
    'Serial/IP configs migrated',
    COUNT(*)
FROM communications c
JOIN comstypes ct ON c.comstypeid = ct.comstypeid
WHERE ct.typename IN ('Serial', 'IP')
UNION ALL
SELECT
    'Warranty records created',
    COUNT(*)
FROM warranties w
WHERE w.machineid IN (SELECT new_machineid FROM pc_to_machine_id_mapping)
UNION ALL
SELECT
    'Dualpath relationships created',
    COUNT(*)
FROM machinerelationships mr
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE rt.relationshiptype = 'Dualpath'
UNION ALL
SELECT
    'Mapping records created',
    COUNT(*)
FROM pc_to_machine_id_mapping
UNION ALL
SELECT
    'Backup tables created',
    COUNT(*)
FROM information_schema.tables
WHERE table_schema = 'shopdb'
AND table_name LIKE '%_backup_phase2';

SELECT '' AS '';

-- Critical issues check
SELECT 'Final critical issues check:' AS '';

SET @critical_issues = 0;

-- Check unmapped PCs
SET @unmapped_pcs = (
    SELECT COUNT(*) FROM pc p
    WHERE p.isactive = 1
      AND NOT EXISTS (SELECT 1 FROM pc_to_machine_id_mapping m WHERE m.pcid = p.pcid)
);

-- Check NULL machineids in communications
SET @null_machineids = (
    SELECT COUNT(*) FROM communications WHERE machineid IS NULL
);

-- Check unmapped interfaces
SET @unmapped_interfaces = (
    SELECT COUNT(*) FROM pc_network_interfaces ni
    WHERE ni.isactive = 1
      AND NOT EXISTS (
        SELECT 1 FROM communications c
        JOIN pc_to_machine_id_mapping m ON c.machineid = m.new_machineid
        WHERE m.pcid = ni.pcid
      )
);

-- Check unmapped configs
SET @unmapped_configs = (
    SELECT COUNT(*) FROM pc_comm_config cc
    WHERE NOT EXISTS (
        SELECT 1 FROM communications c
        JOIN pc_to_machine_id_mapping m ON c.machineid = m.new_machineid
        WHERE m.pcid = cc.pcid
    )
);

SET @critical_issues = @unmapped_pcs + @null_machineids + @unmapped_interfaces + @unmapped_configs;

SELECT
    @unmapped_pcs as unmapped_pcs,
    @unmapped_interfaces as unmapped_interfaces,
    @unmapped_configs as unmapped_configs,
    @null_machineids as null_machineids,
    @critical_issues as total_critical_issues;

SELECT '' AS '';

SELECT CASE
    WHEN @critical_issues = 0 THEN '✓✓✓ PHASE 2 MIGRATION SUCCESSFUL ✓✓✓'
    ELSE '⚠️⚠️⚠️ MIGRATION COMPLETED WITH ISSUES - REVIEW ABOVE ⚠️⚠️⚠️'
END AS status;

SELECT '' AS '';

-- Next steps
SELECT '============================================================' AS '';
SELECT 'NEXT STEPS' AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

SELECT '1. Review verification results above for any warnings' AS '';
SELECT '2. Test application functionality with migrated data' AS '';
SELECT '3. Check backup tables exist and contain expected data' AS '';
SELECT '4. If issues found, use rollback procedures in README.md' AS '';
SELECT '5. If successful, proceed to Phase 3 (view updates)' AS '';
SELECT '' AS '';

SELECT 'Backup tables created (for rollback if needed):' AS '';
SELECT '  - pc_backup_phase2' AS '';
SELECT '  - pc_network_interfaces_backup_phase2' AS '';
SELECT '  - pc_comm_config_backup_phase2' AS '';
SELECT '  - pc_dualpath_assignments_backup_phase2' AS '';
SELECT '' AS '';

SELECT 'Mapping table created (DO NOT DELETE until migration complete):' AS '';
SELECT '  - pc_to_machine_id_mapping' AS '';
SELECT '' AS '';

SELECT '============================================================' AS '';
SELECT 'For detailed information, see migration_phase2/README.md' AS '';
SELECT '============================================================' AS '';

-- =====================================================
-- END OF PHASE 2 MIGRATION
-- =====================================================
