-- =====================================================
-- SCRIPT 07: Migrate Network Communications
-- =====================================================
-- Date: 2025-11-10
-- Purpose: Migrate IP addresses from device tables to communications 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 07' AS '';
SELECT 'Migrating Network Device Communications' AS '';
SELECT '========================================' AS '';

-- Count devices with IP addresses
SELECT 'Servers with IPs:' AS device_type, COUNT(*) AS count
FROM servers WHERE ipaddress IS NOT NULL AND ipaddress != ''
UNION ALL
SELECT 'Switches with IPs:', COUNT(*)
FROM switches WHERE ipaddress IS NOT NULL AND ipaddress != ''
UNION ALL
SELECT 'Cameras with IPs:', COUNT(*)
FROM cameras WHERE ipaddress IS NOT NULL AND ipaddress != '';

-- =====================================================
-- STEP 2: Migrate Server IP Addresses
-- =====================================================

SELECT 'Migrating server IP addresses...' AS status;

INSERT INTO communications (
    machineid,
    comstypeid,
    address,
    macaddress,
    isprimary,
    isactive,
    description
)
SELECT
    m.machineid,
    1 AS comstypeid,  -- IP type
    s.ipaddress AS address,
    NULL AS macaddress,  -- Servers table doesn't have MAC
    1 AS isprimary,
    1 AS isactive,
    'Migrated from servers table' AS description
FROM servers s
JOIN machines m ON m.serialnumber = s.serialnumber AND m.machinetypeid = 30
WHERE s.ipaddress IS NOT NULL
AND s.ipaddress != ''
AND s.isactive = 1
AND NOT EXISTS (
    -- Avoid duplicates
    SELECT 1 FROM communications c
    WHERE c.machineid = m.machineid
    AND c.address = s.ipaddress
);

SELECT 'Server IPs migrated:' AS status, ROW_COUNT() AS migrated_count;

-- =====================================================
-- STEP 3: Migrate Switch IP Addresses
-- =====================================================

SELECT 'Migrating switch IP addresses...' AS status;

INSERT INTO communications (
    machineid,
    comstypeid,
    address,
    macaddress,
    isprimary,
    isactive,
    description
)
SELECT
    m.machineid,
    1 AS comstypeid,  -- IP type
    s.ipaddress AS address,
    NULL AS macaddress,  -- Switches table doesn't have MAC
    1 AS isprimary,
    1 AS isactive,
    'Migrated from switches table' AS description
FROM switches s
JOIN machines m ON m.serialnumber = s.serialnumber AND m.machinetypeid = 31
WHERE s.ipaddress IS NOT NULL
AND s.ipaddress != ''
AND s.isactive = 1
AND NOT EXISTS (
    -- Avoid duplicates
    SELECT 1 FROM communications c
    WHERE c.machineid = m.machineid
    AND c.address = s.ipaddress
);

SELECT 'Switch IPs migrated:' AS status, ROW_COUNT() AS migrated_count;

-- =====================================================
-- STEP 4: Migrate Camera IP Addresses
-- =====================================================

SELECT 'Migrating camera IP addresses...' AS status;

INSERT INTO communications (
    machineid,
    comstypeid,
    address,
    macaddress,
    isprimary,
    isactive,
    description
)
SELECT
    m.machineid,
    1 AS comstypeid,  -- IP type
    c.ipaddress AS address,
    NULL AS macaddress,  -- Cameras table doesn't have MAC
    1 AS isprimary,
    1 AS isactive,
    'Migrated from cameras table' AS description
FROM cameras c
JOIN machines m ON m.serialnumber = c.serialnumber AND m.machinetypeid = 32
WHERE c.ipaddress IS NOT NULL
AND c.ipaddress != ''
AND c.isactive = 1
AND NOT EXISTS (
    -- Avoid duplicates
    SELECT 1 FROM communications comm
    WHERE comm.machineid = m.machineid
    AND comm.address = c.ipaddress
);

SELECT 'Camera IPs migrated:' AS status, ROW_COUNT() AS migrated_count;

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

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

-- Count communications by device type
SELECT 'Communications by device type:' AS status;

SELECT
    mt.machinetype,
    COUNT(DISTINCT m.machineid) AS total_devices,
    COUNT(DISTINCT c.machineid) AS devices_with_ip,
    COUNT(c.comid) AS total_communications
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN communications c ON m.machineid = c.machineid AND c.comstypeid = 1
WHERE mt.machinetypeid BETWEEN 30 AND 36
GROUP BY mt.machinetype
ORDER BY mt.machinetypeid;

-- Show sample communications
SELECT 'Sample network device communications:' AS status;

SELECT
    m.machinenumber,
    mt.machinetype,
    c.address AS ip_address,
    c.isprimary,
    c.description
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
JOIN communications c ON m.machineid = c.machineid
WHERE mt.machinetypeid BETWEEN 30 AND 36
AND c.comstypeid = 1
ORDER BY mt.machinetypeid, m.machinenumber
LIMIT 20;

-- Check for devices without IPs (expected for some)
SELECT 'Devices without IP addresses:' AS status;

SELECT
    mt.machinetype,
    COUNT(*) AS count_without_ip
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE mt.machinetypeid BETWEEN 30 AND 36
AND NOT EXISTS (
    SELECT 1 FROM communications c
    WHERE c.machineid = m.machineid
    AND c.comstypeid = 1
)
GROUP BY mt.machinetype;

SELECT '========================================' AS '';
SELECT '✓ Script 07 completed successfully' AS status;
SELECT 'Next: Run 08_create_network_relationship_types.sql' AS next_step;
SELECT '========================================' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- All IP addresses migrated to communications table with comstypeid = 1 (IP)
-- isprimary = 1 for all migrated IPs (assumed primary interface)
-- MAC addresses not migrated (old tables don't have them)
--
-- To verify a specific device's IP:
-- SELECT m.machinenumber, c.address
-- FROM machines m
-- JOIN communications c ON m.machineid = c.machineid
-- WHERE m.machinetypeid = 30 AND m.machinenumber = 'SVR-0001';
-- =====================================================
