-- =====================================================
-- SCRIPT 02: Migrate Network Interfaces to Communications
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Migrate pc_network_interfaces to communications table
-- Status: REQUIRES TESTING - Depends on Script 01
-- Estimated Time: 3-5 minutes
-- Records: ~705 network interfaces
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Pre-migration checks
-- =====================================================

SELECT '========================================' AS '';
SELECT 'NETWORK INTERFACES MIGRATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Verify pc_to_machine_id_mapping exists
SELECT CASE
    WHEN COUNT(*) > 0 THEN CONCAT('✓ Mapping table exists (', COUNT(*), ' mappings)')
    ELSE '⚠️  ERROR: Run script 01 first!'
END AS status
FROM pc_to_machine_id_mapping;

-- Count interfaces to migrate
SELECT CONCAT('Network interfaces to migrate: ', COUNT(*)) AS info
FROM pc_network_interfaces
WHERE isactive = 1;

SELECT '' AS '';

-- =====================================================
-- STEP 2: Get comstypeid for Network_Interface
-- =====================================================

SET @network_interface_type = (SELECT comstypeid FROM comstypes WHERE typename = 'Network_Interface' LIMIT 1);

SELECT CONCAT('✓ Network_Interface comstypeid: ', @network_interface_type) AS status;
SELECT '' AS '';

-- =====================================================
-- STEP 3: Create backup
-- =====================================================

DROP TABLE IF EXISTS pc_network_interfaces_backup_phase2;
CREATE TABLE pc_network_interfaces_backup_phase2 AS
SELECT * FROM pc_network_interfaces;

SELECT CONCAT('✓ Backup created: ', COUNT(*), ' records') AS status
FROM pc_network_interfaces_backup_phase2;

SELECT '' AS '';

-- =====================================================
-- STEP 4: Show sample data
-- =====================================================

SELECT 'Sample network interfaces (first 5):' AS '';
SELECT
    ni.interfaceid,
    ni.pcid,
    m.new_machineid,
    ni.interfacename,
    ni.ipaddress,
    ni.macaddress,
    ni.isdhcp
FROM pc_network_interfaces ni
LEFT JOIN pc_to_machine_id_mapping m ON ni.pcid = m.pcid
WHERE ni.isactive = 1
LIMIT 5;

SELECT '' AS '';

-- =====================================================
-- STEP 5: Migrate network interfaces
-- =====================================================

SELECT 'Migrating network interfaces...' AS '';

INSERT INTO communications (
    machineid,
    comstypeid,
    address,                    -- IP address
    macaddress,
    subnetmask,
    defaultgateway,
    dnsserver,
    isdhcp,
    interfacename,
    isprimary,
    isactive,
    ismachinenetwork,
    lastupdated
)
SELECT
    m.new_machineid,            -- Map to new machine ID
    @network_interface_type,    -- Network_Interface type
    ni.ipaddress AS address,    -- IP address in generic address field
    ni.macaddress,
    ni.subnetmask,
    ni.defaultgateway,
    NULL AS dnsserver,          -- Not in old schema
    COALESCE(ni.isdhcp, 0),
    ni.interfacename,
    0 AS isprimary,             -- Default to not primary (can be updated later)
    ni.isactive,
    COALESCE(ni.ismachinenetwork, 0),
    ni.lastupdated
FROM pc_network_interfaces ni
JOIN pc_to_machine_id_mapping m ON ni.pcid = m.pcid
WHERE ni.isactive = 1
  AND m.new_machineid IS NOT NULL;

SELECT CONCAT('✓ Migrated ', ROW_COUNT(), ' network interfaces') AS status;

SELECT '' AS '';

-- =====================================================
-- VERIFICATION
-- =====================================================

SELECT '========================================' AS '';
SELECT 'MIGRATION VERIFICATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count by type
SELECT 'Communications by type:' AS '';
SELECT
    ct.typename,
    COUNT(*) as count
FROM communications c
JOIN comstypes ct ON c.comstypeid = ct.comstypeid
GROUP BY ct.typename
ORDER BY count DESC;

SELECT '' AS '';

-- Sample migrated data
SELECT 'Sample migrated communications (first 5):' AS '';
SELECT
    c.comid,
    c.machineid,
    m.machinenumber,
    m.hostname,
    ct.typename,
    c.address,
    c.macaddress
FROM communications c
JOIN machines m ON c.machineid = m.machineid
JOIN comstypes ct ON c.comstypeid = ct.comstypeid
WHERE ct.typename = 'Network_Interface'
LIMIT 5;

SELECT '' AS '';

-- Check for issues
SELECT 'Data quality checks:' AS '';
SELECT
    'Communications with NULL machineid' AS check_name,
    COUNT(*) as count
FROM communications WHERE machineid IS NULL
UNION ALL
SELECT
    'Communications with NULL address' AS check_name,
    COUNT(*) as count
FROM communications WHERE address IS NULL AND comstypeid = @network_interface_type;

SELECT '' AS '';

SELECT '✓ Script 02 completed successfully' AS status;
SELECT 'Next: Run script 03_migrate_comm_config_to_communications.sql' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Backup created: pc_network_interfaces_backup_phase2
--
-- Migrated fields:
-- - pcid → machineid (via mapping table)
-- - ipaddress → address (generic field)
-- - macaddress, subnetmask, defaultgateway → direct copy
-- - interfacename → direct copy
-- - isdhcp, isactive, ismachinenetwork → direct copy
-- - comstypeid set to Network_Interface
--
-- NOT migrated (not in old schema):
-- - port, portname, baud, databits (serial fields)
-- - protocol, username, password (protocol fields)
-- - dnsserver (not in old schema)
--
-- ROLLBACK: Delete from communications where comstypeid = Network_Interface
-- =====================================================
