-- =====================================================
-- SCRIPT 03: Migrate Comm Config to Communications
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Migrate pc_comm_config to communications table
-- Status: REQUIRES TESTING - Depends on Script 01
-- Estimated Time: 2-3 minutes
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

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

SELECT '========================================' AS '';
SELECT 'COMM CONFIG MIGRATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count configs to migrate
SELECT CONCAT('Comm configs to migrate: ', COUNT(*)) AS info
FROM pc_comm_config;

-- Show config types
SELECT 'Config type distribution:' AS '';
SELECT configtype, COUNT(*) as count
FROM pc_comm_config
GROUP BY configtype
ORDER BY count DESC;

SELECT '' AS '';

-- =====================================================
-- STEP 2: Get comstypeid mappings
-- =====================================================

SET @serial_type = (SELECT comstypeid FROM comstypes WHERE typename = 'Serial' LIMIT 1);
SET @ip_type = (SELECT comstypeid FROM comstypes WHERE typename = 'IP' LIMIT 1);

SELECT CONCAT('✓ Serial comstypeid: ', @serial_type) AS status;
SELECT CONCAT('✓ IP comstypeid: ', @ip_type) AS status;
SELECT '' AS '';

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

DROP TABLE IF EXISTS pc_comm_config_backup_phase2;
CREATE TABLE pc_comm_config_backup_phase2 AS
SELECT * FROM pc_comm_config;

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

SELECT '' AS '';

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

SELECT 'Sample comm configs (first 5):' AS '';
SELECT
    cc.configid,
    cc.pcid,
    m.new_machineid,
    cc.configtype,
    cc.portid,
    cc.baud,
    cc.ipaddress,
    cc.socketnumber
FROM pc_comm_config cc
LEFT JOIN pc_to_machine_id_mapping m ON cc.pcid = m.pcid
LIMIT 5;

SELECT '' AS '';

-- =====================================================
-- STEP 5: Migrate comm configs
-- =====================================================

SELECT 'Migrating comm configs...' AS '';

INSERT INTO communications (
    machineid,
    comstypeid,
    address,                    -- IP address OR port name
    port,                       -- Socket number for IP, NULL for serial
    portname,                   -- COM port name for serial
    baud,
    databits,
    stopbits,
    parity,
    flowcontrol,
    settings,                   -- Additional settings as text
    isactive,
    lastupdated
)
SELECT
    m.new_machineid,
    CASE
        WHEN cc.configtype = 'Serial' THEN @serial_type
        WHEN cc.configtype = 'IP' THEN @ip_type
        ELSE @ip_type  -- Default to IP
    END AS comstypeid,
    CASE
        WHEN cc.configtype = 'Serial' THEN cc.portid  -- COM1, COM2, etc.
        WHEN cc.configtype = 'IP' THEN cc.ipaddress
        ELSE cc.ipaddress
    END AS address,
    cc.socketnumber AS port,    -- Port number for IP connections
    CASE
        WHEN cc.configtype = 'Serial' THEN cc.portid
        ELSE NULL
    END AS portname,
    cc.baud,
    cc.databits,
    cc.stopbits,
    cc.parity,
    NULL AS flowcontrol,        -- Not in old schema, set CRLF to settings
    CONCAT(
        CASE WHEN cc.crlf IS NOT NULL THEN CONCAT('CRLF: ', cc.crlf, '; ') ELSE '' END,
        CASE WHEN cc.additionalsettings IS NOT NULL THEN cc.additionalsettings ELSE '' END
    ) AS settings,
    1 AS isactive,
    cc.lastupdated
FROM pc_comm_config cc
JOIN pc_to_machine_id_mapping m ON cc.pcid = m.pcid
WHERE m.new_machineid IS NOT NULL;

SELECT CONCAT('✓ Migrated ', ROW_COUNT(), ' comm configs') AS status;

SELECT '' AS '';

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

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

-- Count by type
SELECT 'Communications by type (all):' 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 serial configs
SELECT 'Sample serial communications:' AS '';
SELECT
    c.comid,
    m.hostname,
    c.address AS port,
    c.portname,
    c.baud,
    c.databits,
    c.stopbits,
    c.parity
FROM communications c
JOIN machines m ON c.machineid = m.machineid
JOIN comstypes ct ON c.comstypeid = ct.comstypeid
WHERE ct.typename = 'Serial'
LIMIT 3;

SELECT '' AS '';

-- Sample IP configs
SELECT 'Sample IP communications:' AS '';
SELECT
    c.comid,
    m.hostname,
    c.address AS ipaddress,
    c.port AS socket
FROM communications c
JOIN machines m ON c.machineid = m.machineid
JOIN comstypes ct ON c.comstypeid = ct.comstypeid
WHERE ct.typename = 'IP'
LIMIT 3;

SELECT '' AS '';

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

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Backup created: pc_comm_config_backup_phase2
--
-- Migrated fields:
-- - Serial configs: address = portid (COM1, COM2, etc.)
-- - IP configs: address = ipaddress, port = socketnumber
-- - baud, databits, stopbits, parity → direct copy
-- - crlf + additionalsettings → combined in settings field
--
-- Mapping:
-- - configtype 'Serial' → comstypeid for 'Serial'
-- - configtype 'IP' → comstypeid for 'IP'
--
-- ROLLBACK: Delete from communications where comstypeid IN (Serial, IP)
-- =====================================================
