-- =====================================================
-- MASTER SCRIPT: Run All Phase 1 Migration Scripts
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Execute all 8 Phase 1 scripts in correct order
-- Estimated Time: 15-20 minutes
-- WARNING: Creates 7 new tables, modifies 3 existing tables
-- =====================================================

-- =====================================================
-- PRE-FLIGHT CHECKS
-- =====================================================

SELECT '========================================' AS '';
SELECT 'PHASE 1 MIGRATION - MASTER SCRIPT' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';
SELECT CONCAT('Start Time: ', NOW()) AS '';
SELECT CONCAT('Database: ', DATABASE()) AS '';
SELECT CONCAT('User: ', USER()) AS '';
SELECT '' AS '';

-- Check if we're in the right database
SELECT CASE
    WHEN DATABASE() = 'shopdb' THEN '✓ Correct database (shopdb)'
    ELSE '⚠️  WARNING: Not in shopdb database!'
END AS 'Database Check';

-- Check if pc table exists (needed for later migration)
SELECT CASE
    WHEN COUNT(*) > 0 THEN CONCAT('✓ pc table exists (', COUNT(*), ' records)')
    ELSE '⚠️  WARNING: pc table not found!'
END AS 'PC Table Check'
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'shopdb' AND TABLE_NAME = 'pc';

-- Check if machines table exists
SELECT CASE
    WHEN COUNT(*) > 0 THEN '✓ machines table exists'
    ELSE '⚠️  ERROR: machines table not found!'
END AS 'Machines Table Check'
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'shopdb' AND TABLE_NAME = 'machines';

SELECT '' AS '';
SELECT 'Press Ctrl+C to cancel, or continue executing...' AS '';
SELECT 'Creating backup recommended before proceeding!' AS '';
SELECT '' AS '';

-- Pause (you'll need to manually run each section if you want to pause)

-- =====================================================
-- SCRIPT 01: Communications Infrastructure
-- =====================================================

SELECT '========================================' AS '';
SELECT 'SCRIPT 01: Communications Infrastructure' AS '';
SELECT '========================================' AS '';

SOURCE 01_create_communications_infrastructure.sql;

SELECT '' AS '';

-- =====================================================
-- SCRIPT 02: Extend Machines Table
-- =====================================================

SELECT '========================================' AS '';
SELECT 'SCRIPT 02: Extend Machines Table' AS '';
SELECT '========================================' AS '';

SOURCE 02_extend_machines_table.sql;

SELECT '' AS '';

-- =====================================================
-- SCRIPT 03: Create PC Machine Types
-- =====================================================

SELECT '========================================' AS '';
SELECT 'SCRIPT 03: Create PC Machine Types' AS '';
SELECT '========================================' AS '';

SOURCE 03_create_pc_machine_types.sql;

SELECT '' AS '';

-- =====================================================
-- SCRIPT 04: Warranty Infrastructure
-- =====================================================

SELECT '========================================' AS '';
SELECT 'SCRIPT 04: Warranty Infrastructure' AS '';
SELECT '========================================' AS '';

SOURCE 04_create_warranty_infrastructure.sql;

SELECT '' AS '';

-- =====================================================
-- SCRIPT 05: Compliance Infrastructure
-- =====================================================

SELECT '========================================' AS '';
SELECT 'SCRIPT 05: Compliance Infrastructure' AS '';
SELECT '========================================' AS '';

SOURCE 05_create_compliance_infrastructure.sql;

SELECT '' AS '';

-- =====================================================
-- SCRIPT 06: Extend Businessunits Table
-- =====================================================

SELECT '========================================' AS '';
SELECT 'SCRIPT 06: Extend Businessunits Table' AS '';
SELECT '========================================' AS '';

SOURCE 06_extend_businessunits_table.sql;

SELECT '' AS '';

-- =====================================================
-- SCRIPT 07: Rename pcstatus to machinestatus
-- =====================================================

SELECT '========================================' AS '';
SELECT 'SCRIPT 07: Rename pcstatus to machinestatus' AS '';
SELECT '========================================' AS '';

SOURCE 07_rename_pcstatus_to_machinestatus.sql;

SELECT '' AS '';

-- =====================================================
-- SCRIPT 08: Machine Relationships Infrastructure
-- =====================================================

SELECT '========================================' AS '';
SELECT 'SCRIPT 08: Machine Relationships' AS '';
SELECT '========================================' AS '';

SOURCE 08_create_machine_relationships_infrastructure.sql;

SELECT '' AS '';

-- =====================================================
-- POST-EXECUTION VERIFICATION
-- =====================================================

SELECT '========================================' AS '';
SELECT 'POST-EXECUTION VERIFICATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count new tables
SELECT 'New Tables Created:' AS '';
SELECT TABLE_NAME, TABLE_ROWS
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'shopdb'
  AND TABLE_NAME IN ('comstypes', 'communications', 'warranties',
                     'compliance', 'compliancescans', 'relationshiptypes',
                     'machinerelationships', 'machinestatus')
ORDER BY TABLE_NAME;

SELECT '' AS '';

-- Count new views
SELECT 'New Views Created:' AS '';
SELECT TABLE_NAME
FROM information_schema.VIEWS
WHERE TABLE_SCHEMA = 'shopdb'
  AND TABLE_NAME LIKE 'vw_%warrant%'
   OR TABLE_NAME LIKE 'vw_%compliance%'
   OR TABLE_NAME LIKE 'vw_%relationship%'
   OR TABLE_NAME LIKE 'vw_dualpath%'
ORDER BY TABLE_NAME;

SELECT '' AS '';

-- Check machines table columns
SELECT 'Machines Table Columns:' AS '';
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'shopdb'
  AND TABLE_NAME = 'machines'
  AND COLUMN_NAME IN ('hostname', 'serialnumber', 'osid', 'machinestatusid',
                      'pctypeid', 'controllertypeid', 'controllerosid',
                      'lastupdated', 'dateadded', 'requires_manual_machine_config',
                      'loggedinuser')
ORDER BY ORDINAL_POSITION;

SELECT '' AS '';

-- Check PC machine types
SELECT 'PC Machine Types Created:' AS '';
SELECT machinetypeid, machinetype, machinedescription
FROM machinetypes
WHERE machinetype LIKE 'PC -%'
ORDER BY machinetype;

SELECT '' AS '';

-- =====================================================
-- COMPLETION SUMMARY
-- =====================================================

SELECT '========================================' AS '';
SELECT '✓ PHASE 1 MIGRATION COMPLETE!' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';
SELECT CONCAT('End Time: ', NOW()) AS '';
SELECT '' AS '';
SELECT 'RESULTS:' AS '';
SELECT '- 7 new tables created' AS '';
SELECT '- 3 tables modified' AS '';
SELECT '- 5+ views created' AS '';
SELECT '- 5 PC machine types added' AS '';
SELECT '- 11 columns added to machines table' AS '';
SELECT '- 2 columns added to businessunits table' AS '';
SELECT '' AS '';
SELECT 'NEXT STEPS:' AS '';
SELECT '1. Review verification output above' AS '';
SELECT '2. Test queries on new tables' AS '';
SELECT '3. Proceed to Phase 2 (data migration)' AS '';
SELECT '' AS '';
SELECT 'ROLLBACK:' AS '';
SELECT '- Run ROLLBACK scripts in reverse order (08 down to 01)' AS '';
SELECT '- Or restore from backup' AS '';
SELECT '' AS '';

-- =====================================================
-- NOTES
-- =====================================================
-- This master script executes all 8 Phase 1 scripts.
-- Each script is REVERSIBLE using its corresponding ROLLBACK script.
--
-- To run this script:
-- docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < RUN_ALL_PHASE1_SCRIPTS.sql
--
-- To rollback all changes:
-- Run ROLLBACK scripts in reverse order (08, 07, 06, 05, 04, 03, 02, 01)
-- =====================================================
