-- =====================================================
-- SCRIPT 02: Migrate Servers to machines Table
-- =====================================================
-- Date: 2025-11-10
-- Purpose: Migrate all servers from servers 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 02' AS '';
SELECT 'Migrating Servers to machines Table' AS '';
SELECT '========================================' AS '';

-- Count servers to migrate
SELECT 'Pre-migration server count:' AS status, COUNT(*) AS server_count
FROM servers
WHERE isactive = 1;

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

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

-- If conflicts found, show them
SELECT s.serverid, s.serialnumber, s.description
FROM servers s
WHERE EXISTS (
    SELECT 1 FROM machines m
    WHERE m.serialnumber = s.serialnumber
    AND m.machinetypeid = 30
)
LIMIT 10;

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

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

CREATE TEMPORARY TABLE IF NOT EXISTS temp_server_mapping (
    old_serverid INT,
    new_machineid INT,
    PRIMARY KEY (old_serverid)
);

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

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

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

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

    -- modelnumberid
    s.modelid AS modelnumberid,

    -- machinetypeid = 30 (Server)
    30 AS machinetypeid,

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

    -- serialnumber
    s.serialnumber,

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

    -- map coordinates (servers table doesn't have map fields)
    NULL AS mapleft,
    NULL AS maptop,

    -- isactive
    s.isactive,

    -- dateadded - servers table doesn't have this column
    NOW() AS dateadded,

    -- lastupdated
    NOW() AS lastupdated
FROM servers 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 = 30
);

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

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

INSERT INTO temp_server_mapping (old_serverid, new_machineid)
SELECT
    s.serverid,
    m.machineid
FROM servers s
JOIN machines m ON m.serialnumber = s.serialnumber AND m.machinetypeid = 30
WHERE s.isactive = 1;

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

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

-- Count migrated servers
SELECT 'Servers in machines table:' AS status, COUNT(*) AS migrated_count
FROM machines
WHERE machinetypeid = 30;

-- Show sample of migrated servers
SELECT 'Sample migrated servers:' 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 = 30
ORDER BY m.machineid
LIMIT 10;

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

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

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Old servers table is NOT dropped - kept for rollback safety
-- Mapping table temp_server_mapping persists for this session
-- Will be used by later scripts to migrate communications and relationships
--
-- To verify a specific server migrated:
-- SELECT * FROM machines WHERE machinetypeid = 30 AND serialnumber = 'YOUR_SERIAL';
--
-- To see old vs new:
-- SELECT s.*, m.machineid, m.machinenumber
-- FROM servers s
-- JOIN machines m ON s.serialnumber = m.serialnumber AND m.machinetypeid = 30
-- WHERE s.serverid = YOUR_SERVERID;
-- =====================================================
