-- =====================================================
-- ROLLBACK: Phase 3 Migration
-- =====================================================
-- Date: 2025-11-10
-- Purpose: Rollback Phase 3 migration (Network Devices)
-- USE WITH CAUTION: This will delete migrated data
-- =====================================================
--
-- IMPORTANT: READ BEFORE RUNNING
-- =====================================================
-- 1. This script removes all network devices from machines table
-- 2. Old tables (servers, switches, cameras) must still exist
-- 3. Only run if migration failed or issues found
-- 4. Backup database before rollback!
-- 5. Cannot rollback if old tables were dropped
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

SELECT '============================================================' AS '';
SELECT 'PHASE 3 MIGRATION ROLLBACK' AS '';
SELECT '============================================================' AS '';
SELECT 'Start Time:' AS '', NOW() AS timestamp;
SELECT '' AS '';

-- =====================================================
-- STEP 1: Verification - Check Old Tables Exist
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Step 1: Verifying old tables still exist' AS '';
SELECT '------------------------------------------------------------' AS '';

-- Check if old tables exist
SELECT
    CASE
        WHEN COUNT(*) = 3 THEN 'PASS - All old tables exist'
        ELSE 'FAIL - Some old tables missing! Cannot rollback safely.'
    END AS status,
    COUNT(*) AS tables_found
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'shopdb'
AND TABLE_NAME IN ('servers', 'switches', 'cameras');

-- Show old table record counts
SELECT 'Old table record counts:' AS status;
SELECT 'servers' AS table_name, COUNT(*) AS record_count FROM servers
UNION ALL
SELECT 'switches', COUNT(*) FROM switches
UNION ALL
SELECT 'cameras', COUNT(*) FROM cameras;

-- =====================================================
-- STEP 2: Show What Will Be Deleted
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Step 2: Records that will be removed from machines table' AS '';
SELECT '------------------------------------------------------------' AS '';

SELECT
    mt.machinetype AS device_type,
    COUNT(*) AS records_to_delete
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE mt.machinetypeid BETWEEN 30 AND 36
GROUP BY mt.machinetype
ORDER BY mt.machinetypeid;

-- Show sample records to be deleted
SELECT 'Sample records to be deleted:' AS status;
SELECT
    m.machineid,
    m.machinenumber,
    mt.machinetype,
    m.serialnumber
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE mt.machinetypeid BETWEEN 30 AND 36
LIMIT 10;

-- =====================================================
-- STEP 3: Confirmation Prompt
-- =====================================================

SELECT '' AS '';
SELECT '============================================================' AS '';
SELECT 'CONFIRMATION REQUIRED' AS '';
SELECT '============================================================' AS '';
SELECT 'This will DELETE all network devices from machines table' AS '';
SELECT 'Old data in servers/switches/cameras tables will remain' AS '';
SELECT '' AS '';
SELECT 'To proceed, uncomment the rollback steps below' AS '';
SELECT 'and run this script again.' AS '';
SELECT '============================================================' AS '';

-- =====================================================
-- STEP 4: Delete Communications for Network Devices
-- =====================================================
-- UNCOMMENT TO EXECUTE ROLLBACK

/*
SELECT '------------------------------------------------------------' AS '';
SELECT 'Step 4: Deleting communications for network devices' AS '';
SELECT '------------------------------------------------------------' AS '';

DELETE c
FROM communications c
JOIN machines m ON c.machineid = m.machineid
WHERE m.machinetypeid BETWEEN 30 AND 36;

SELECT 'Communications deleted:' AS status, ROW_COUNT() AS deleted_count;
*/

-- =====================================================
-- STEP 5: Delete Relationships Involving Network Devices
-- =====================================================
-- UNCOMMENT TO EXECUTE ROLLBACK

/*
SELECT '------------------------------------------------------------' AS '';
SELECT 'Step 5: Deleting relationships involving network devices' AS '';
SELECT '------------------------------------------------------------' AS '';

DELETE FROM machinerelationships
WHERE machineid IN (
    SELECT machineid FROM machines WHERE machinetypeid BETWEEN 30 AND 36
)
OR related_machineid IN (
    SELECT machineid FROM machines WHERE machinetypeid BETWEEN 30 AND 36
);

SELECT 'Relationships deleted:' AS status, ROW_COUNT() AS deleted_count;
*/

-- =====================================================
-- STEP 6: Delete Network Devices from machines Table
-- =====================================================
-- UNCOMMENT TO EXECUTE ROLLBACK

/*
SELECT '------------------------------------------------------------' AS '';
SELECT 'Step 6: Deleting network devices from machines table' AS '';
SELECT '------------------------------------------------------------' AS '';

DELETE FROM machines
WHERE machinetypeid BETWEEN 30 AND 36;

SELECT 'Machines deleted:' AS status, ROW_COUNT() AS deleted_count;
*/

-- =====================================================
-- STEP 7: Remove Network Relationship Types
-- =====================================================
-- UNCOMMENT TO EXECUTE ROLLBACK

/*
SELECT '------------------------------------------------------------' AS '';
SELECT 'Step 7: Removing network relationship types' AS '';
SELECT '------------------------------------------------------------' AS '';

DELETE FROM relationshiptypes
WHERE relationshiptype IN (
    'Connected To',
    'Powered By',
    'Mounted In',
    'Feeds Video To',
    'Provides Network'
);

SELECT 'Relationship types deleted:' AS status, ROW_COUNT() AS deleted_count;
*/

-- =====================================================
-- STEP 8: Drop Views
-- =====================================================
-- UNCOMMENT TO EXECUTE ROLLBACK

/*
SELECT '------------------------------------------------------------' AS '';
SELECT 'Step 8: Dropping network device views' AS '';
SELECT '------------------------------------------------------------' AS '';

DROP VIEW IF EXISTS vw_all_infrastructure;
DROP VIEW IF EXISTS vw_network_devices_summary;
DROP VIEW IF EXISTS vw_network_topology;

SELECT 'Views dropped' AS status;
*/

-- =====================================================
-- STEP 9: Remove Network Machine Types
-- =====================================================
-- OPTIONAL: Usually safe to keep these for future use
-- UNCOMMENT TO EXECUTE ROLLBACK

/*
SELECT '------------------------------------------------------------' AS '';
SELECT 'Step 9: Removing network machine types (OPTIONAL)' AS '';
SELECT '------------------------------------------------------------' AS '';

DELETE FROM machinetypes
WHERE machinetypeid BETWEEN 30 AND 36;

SELECT 'Machine types deleted:' AS status, ROW_COUNT() AS deleted_count;
*/

-- =====================================================
-- STEP 10: Verification After Rollback
-- =====================================================
-- UNCOMMENT TO EXECUTE ROLLBACK

/*
SELECT '============================================================' AS '';
SELECT 'ROLLBACK VERIFICATION' AS '';
SELECT '============================================================' AS '';

-- Verify machines table cleaned up
SELECT 'Network devices remaining in machines:' AS test,
    CASE
        WHEN COUNT(*) = 0 THEN 'SUCCESS - All network devices removed'
        ELSE CONCAT('WARNING - ', COUNT(*), ' network devices still remain')
    END AS result
FROM machines
WHERE machinetypeid BETWEEN 30 AND 36;

-- Verify old tables still intact
SELECT 'Old tables status:' AS status;
SELECT 'servers' AS table_name, COUNT(*) AS record_count FROM servers
UNION ALL
SELECT 'switches', COUNT(*) FROM switches
UNION ALL
SELECT 'cameras', COUNT(*) FROM cameras;

SELECT '' AS '';
SELECT '✓ Rollback complete' AS status;
SELECT 'Old tables remain intact for re-migration' AS note;
*/

-- =====================================================
-- COMPLETION
-- =====================================================

SELECT '' AS '';
SELECT '============================================================' AS '';
SELECT 'End Time:' AS '', NOW() AS timestamp;
SELECT '============================================================' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- POST-ROLLBACK STEPS
-- =====================================================
-- After successful rollback:
-- 1. Review what caused the need for rollback
-- 2. Fix issues in migration scripts
-- 3. Test on backup database
-- 4. Re-run migration when ready
--
-- To re-run migration:
-- SOURCE RUN_ALL_PHASE3_SCRIPTS.sql;
-- =====================================================

-- =====================================================
-- EMERGENCY RESTORE (if old tables were dropped)
-- =====================================================
-- If old tables were dropped, restore from backup:
-- mysql -u root -p shopdb < shopdb_backup_before_phase3.sql
-- =====================================================
