-- ============================================================================
-- ShopDB Infrastructure Enhancement - ROLLBACK SCRIPT
-- ============================================================================
-- Purpose: Undo all changes from PRODUCTION_READY_infrastructure_migration.sql
-- Author: Development Team
-- Date: 2025-10-22
-- Version: 1.0
--
-- WARNING: This will remove all infrastructure enhancements and printer coordinates!
-- Only run this if you need to completely undo the migration.
-- ============================================================================

-- Set session variables for safety
SET SQL_SAFE_UPDATES = 0;
SET FOREIGN_KEY_CHECKS = 0;

SELECT '============================================' AS separator;
SELECT 'STARTING ROLLBACK OF INFRASTRUCTURE MIGRATION' AS status;
SELECT '============================================' AS separator;


-- ============================================================================
-- SECTION 1: DROP VIEWS CREATED BY MIGRATION
-- ============================================================================

DROP VIEW IF EXISTS vw_infrastructure_summary;
DROP VIEW IF EXISTS vw_network_devices;
DROP VIEW IF EXISTS vw_idf_inventory;

SELECT 'Step 1 Complete: Dropped infrastructure views' AS status;


-- ============================================================================
-- SECTION 2: REMOVE COLUMNS FROM CAMERAS TABLE
-- ============================================================================

-- Remove foreign key constraint first
ALTER TABLE cameras DROP FOREIGN KEY IF EXISTS fk_cameras_idf;

-- Drop indexes
ALTER TABLE cameras DROP INDEX IF EXISTS idx_idfid;
ALTER TABLE cameras DROP INDEX IF EXISTS idx_serialnumber;
ALTER TABLE cameras DROP INDEX IF EXISTS idx_macaddress;

-- Remove columns
ALTER TABLE cameras DROP COLUMN IF EXISTS idfid;
ALTER TABLE cameras DROP COLUMN IF EXISTS serialnumber;
ALTER TABLE cameras DROP COLUMN IF EXISTS macaddress;

-- Re-add hostname column
ALTER TABLE cameras ADD COLUMN hostname VARCHAR(100) AFTER cameraid;

SELECT 'Step 2 Complete: Rolled back cameras table changes' AS status;


-- ============================================================================
-- SECTION 3: REMOVE COLUMNS FROM SERVERS TABLE
-- ============================================================================

-- Drop indexes
ALTER TABLE servers DROP INDEX IF EXISTS idx_serialnumber;

-- Remove columns
ALTER TABLE servers DROP COLUMN IF EXISTS serialnumber;

-- Re-add hostname column
ALTER TABLE servers ADD COLUMN hostname VARCHAR(100) AFTER serverid;

SELECT 'Step 3 Complete: Rolled back servers table changes' AS status;


-- ============================================================================
-- SECTION 4: REMOVE COLUMNS FROM ACCESSPOINTS TABLE
-- ============================================================================

-- Drop indexes
ALTER TABLE accesspoints DROP INDEX IF EXISTS idx_serialnumber;

-- Remove columns
ALTER TABLE accesspoints DROP COLUMN IF EXISTS serialnumber;

-- Re-add hostname and timestamp columns
ALTER TABLE accesspoints ADD COLUMN hostname VARCHAR(100) AFTER apid;
ALTER TABLE accesspoints ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP AFTER isactive;
ALTER TABLE accesspoints ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER created_at;

SELECT 'Step 4 Complete: Rolled back accesspoints table changes' AS status;


-- ============================================================================
-- SECTION 5: REMOVE COLUMNS FROM SWITCHES TABLE
-- ============================================================================

-- Drop indexes
ALTER TABLE switches DROP INDEX IF EXISTS idx_serialnumber;

-- Remove columns
ALTER TABLE switches DROP COLUMN IF EXISTS serialnumber;

-- Re-add hostname column
ALTER TABLE switches ADD COLUMN hostname VARCHAR(100) AFTER switchid;

SELECT 'Step 5 Complete: Rolled back switches table changes' AS status;


-- ============================================================================
-- SECTION 6: ROLLBACK IDFs TABLE STRUCTURE
-- ============================================================================

-- Drop index
ALTER TABLE idfs DROP INDEX IF EXISTS idx_idfname;

-- Remove idfname column
ALTER TABLE idfs DROP COLUMN IF EXISTS idfname;

-- Re-add hostname and ipaddress columns
ALTER TABLE idfs ADD COLUMN hostname VARCHAR(100) AFTER idfid;
ALTER TABLE idfs ADD COLUMN ipaddress VARCHAR(45) AFTER hostname;

SELECT 'Step 6 Complete: Rolled back IDFs table changes' AS status;


-- ============================================================================
-- SECTION 7: REMOVE PRINTER COORDINATES
-- ============================================================================

-- Drop the maptop and mapleft columns from printers
-- NOTE: This will delete all printer coordinate data!
ALTER TABLE printers DROP COLUMN IF EXISTS maptop;
ALTER TABLE printers DROP COLUMN IF EXISTS mapleft;

SELECT 'Step 7 Complete: Removed printer coordinate columns' AS status;


-- ============================================================================
-- SECTION 8: VERIFICATION
-- ============================================================================

SELECT '============================================' AS separator;
SELECT 'ROLLBACK VERIFICATION' AS report_title;
SELECT '============================================' AS separator;

-- Verify columns were removed
SELECT 'Verifying columns were removed:' AS note;

-- Check printers table (should NOT have maptop/mapleft)
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN '✓ Printers: maptop removed'
        ELSE '✗ ERROR: Printers still has maptop column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'printers'
  AND COLUMN_NAME = 'maptop';

SELECT
    CASE
        WHEN COUNT(*) = 0 THEN '✓ Printers: mapleft removed'
        ELSE '✗ ERROR: Printers still has mapleft column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'printers'
  AND COLUMN_NAME = 'mapleft';

-- Check IDFs table (should have hostname/ipaddress, NOT idfname)
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN '✓ IDFs: idfname removed'
        ELSE '✗ ERROR: IDFs still has idfname column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'idfs'
  AND COLUMN_NAME = 'idfname';

SELECT
    CASE
        WHEN COUNT(*) = 1 THEN '✓ IDFs: hostname restored'
        ELSE '✗ ERROR: IDFs missing hostname column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'idfs'
  AND COLUMN_NAME = 'hostname';

-- Check cameras table (should have hostname, NOT idfid/serialnumber/macaddress)
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN '✓ Cameras: idfid removed'
        ELSE '✗ ERROR: Cameras still has idfid column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'cameras'
  AND COLUMN_NAME = 'idfid';

SELECT
    CASE
        WHEN COUNT(*) = 1 THEN '✓ Cameras: hostname restored'
        ELSE '✗ ERROR: Cameras missing hostname column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'cameras'
  AND COLUMN_NAME = 'hostname';

-- Check switches table (should have hostname, NOT serialnumber)
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN '✓ Switches: serialnumber removed'
        ELSE '✗ ERROR: Switches still has serialnumber column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'switches'
  AND COLUMN_NAME = 'serialnumber';

SELECT
    CASE
        WHEN COUNT(*) = 1 THEN '✓ Switches: hostname restored'
        ELSE '✗ ERROR: Switches missing hostname column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'switches'
  AND COLUMN_NAME = 'hostname';

-- Check accesspoints table (should have hostname and timestamps, NOT serialnumber)
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN '✓ Access Points: serialnumber removed'
        ELSE '✗ ERROR: Access Points still has serialnumber column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'accesspoints'
  AND COLUMN_NAME = 'serialnumber';

SELECT
    CASE
        WHEN COUNT(*) = 1 THEN '✓ Access Points: hostname restored'
        ELSE '✗ ERROR: Access Points missing hostname column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'accesspoints'
  AND COLUMN_NAME = 'hostname';

SELECT
    CASE
        WHEN COUNT(*) = 1 THEN '✓ Access Points: created_at restored'
        ELSE '✗ ERROR: Access Points missing created_at column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'accesspoints'
  AND COLUMN_NAME = 'created_at';

-- Check servers table (should have hostname, NOT serialnumber)
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN '✓ Servers: serialnumber removed'
        ELSE '✗ ERROR: Servers still has serialnumber column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'servers'
  AND COLUMN_NAME = 'serialnumber';

SELECT
    CASE
        WHEN COUNT(*) = 1 THEN '✓ Servers: hostname restored'
        ELSE '✗ ERROR: Servers missing hostname column'
    END AS verification
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'servers'
  AND COLUMN_NAME = 'hostname';

-- Verify views were dropped
SELECT
    CASE
        WHEN COUNT(*) = 0 THEN '✓ All infrastructure views removed'
        ELSE CONCAT('✗ ERROR: ', COUNT(*), ' views still exist')
    END AS verification
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME IN ('vw_infrastructure_summary', 'vw_network_devices', 'vw_idf_inventory');


-- ============================================================================
-- SECTION 9: FINAL STATUS
-- ============================================================================

-- Re-enable safety features
SET FOREIGN_KEY_CHECKS = 1;
SET SQL_SAFE_UPDATES = 1;

SELECT '============================================' AS separator;
SELECT 'ROLLBACK COMPLETED' AS final_status;
SELECT '============================================' AS separator;

SELECT 'Database restored to pre-migration state' AS note;
SELECT 'All infrastructure enhancements have been removed' AS warning;
SELECT 'Printer coordinates have been deleted' AS data_loss_warning;

-- ============================================================================
-- END OF ROLLBACK SCRIPT
-- ============================================================================
