-- =====================================================
-- SCRIPT 03: Migrate Switches to machines Table
-- =====================================================
-- Date: 2025-11-10
-- Purpose: Migrate all switches from switches table to machines table
-- Part of: Phase 3 Migration (Network Devices → machines table)
-- Status: REVERSIBLE (see ROLLBACK script)
-- Estimated Time: 1-2 minutes
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Pre-Migration Verification
-- =====================================================

SELECT '========================================' AS '';
SELECT 'Phase 3 Migration - Script 03' AS '';
SELECT 'Migrating Switches to machines Table' AS '';
SELECT '========================================' AS '';

-- Count switches to migrate
SELECT 'Pre-migration switch count:' AS status, COUNT(*) AS switch_count
FROM switches
WHERE isactive = 1;

-- Check for any conflicts
SELECT 'Checking for conflicts...' AS status;

SELECT COUNT(*) AS potential_conflicts
FROM switches s
WHERE EXISTS (
    SELECT 1 FROM machines m
    WHERE m.serialnumber = s.serialnumber
    AND m.machinetypeid = 31
);

-- =====================================================
-- STEP 2: Create Temporary Mapping Table
-- =====================================================

SELECT 'Creating switch mapping table...' AS status;

CREATE TEMPORARY TABLE IF NOT EXISTS temp_switch_mapping (
    old_switchid INT,
    new_machineid INT,
    PRIMARY KEY (old_switchid)
);

-- =====================================================
-- STEP 3: Migrate Switches to machines Table
-- =====================================================

SELECT 'Migrating switches to machines table...' AS status;

INSERT INTO machines (
    machinenumber,
    alias,
    modelnumberid,
    machinetypeid,
    pctypeid,
    serialnumber,
    machinenotes,
    mapleft,
    maptop,
    isactive,
    dateadded,
    lastupdated
)
SELECT
    -- Generate machinenumber: SW-{switchid} or use description
    CASE
        WHEN s.description IS NOT NULL AND s.description != ''
        THEN CONCAT('SW-', LPAD(s.switchid, 4, '0'))
        ELSE CONCAT('SWITCH-', s.switchid)
    END AS machinenumber,

    -- alias from description
    COALESCE(s.description, '') AS alias,

    -- modelnumberid
    s.modelid AS modelnumberid,

    -- machinetypeid = 31 (Switch)
    31 AS machinetypeid,

    -- pctypeid = NULL (not a PC)
    NULL AS pctypeid,

    -- serialnumber
    s.serialnumber,

    -- machinenotes (empty for now)
    '' AS machinenotes,

    -- map coordinates
    COALESCE(s.mapleft, 50) AS mapleft,
    COALESCE(s.maptop, 50) AS maptop,

    -- isactive
    s.isactive,

    -- dateadded - preserve or use current time
    NOW() AS dateadded,

    -- lastupdated
    NOW() AS lastupdated
FROM switches s
WHERE s.isactive = 1
AND NOT EXISTS (
    -- Avoid duplicates if script run multiple times
    SELECT 1 FROM machines m
    WHERE m.serialnumber = s.serialnumber
    AND m.machinetypeid = 31
);

-- =====================================================
-- STEP 4: Create Mapping from Old to New IDs
-- =====================================================

SELECT 'Creating ID mapping...' AS status;

INSERT INTO temp_switch_mapping (old_switchid, new_machineid)
SELECT
    s.switchid,
    m.machineid
FROM switches s
JOIN machines m ON m.serialnumber = s.serialnumber AND m.machinetypeid = 31
WHERE s.isactive = 1;

-- =====================================================
-- STEP 5: Verification
-- =====================================================

SELECT '========================================' AS '';
SELECT 'Migration Verification:' AS '';
SELECT '========================================' AS '';

-- Count migrated switches
SELECT 'Switches in machines table:' AS status, COUNT(*) AS migrated_count
FROM machines
WHERE machinetypeid = 31;

-- Show sample of migrated switches
SELECT 'Sample migrated switches:' AS status;

SELECT
    m.machineid,
    m.machinenumber,
    m.alias,
    m.serialnumber,
    mo.modelnumber,
    v.vendor
FROM machines m
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
WHERE m.machinetypeid = 31
ORDER BY m.machineid
LIMIT 10;

-- Show mapping stats
SELECT 'ID mapping created:' AS status, COUNT(*) AS mapping_count
FROM temp_switch_mapping;

-- Summary of all network devices so far
SELECT 'Current network device totals:' AS status;
SELECT
    mt.machinetype,
    COUNT(*) AS count
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;

SELECT '========================================' AS '';
SELECT '✓ Script 03 completed successfully' AS status;
SELECT 'Next: Run 04_migrate_cameras_to_machines.sql' AS next_step;
SELECT '========================================' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Old switches table is NOT dropped - kept for rollback safety
-- Mapping table temp_switch_mapping persists for this session
-- Will be used by later scripts to migrate communications and relationships
-- =====================================================
