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

-- Count cameras to migrate
SELECT 'Pre-migration camera count:' AS status, COUNT(*) AS camera_count
FROM cameras
WHERE isactive = 1;

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

SELECT COUNT(*) AS potential_conflicts
FROM cameras c
WHERE EXISTS (
    SELECT 1 FROM machines m
    WHERE m.serialnumber = c.serialnumber
    AND m.machinetypeid = 32
);

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

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

CREATE TEMPORARY TABLE IF NOT EXISTS temp_camera_mapping (
    old_cameraid INT,
    new_machineid INT,
    PRIMARY KEY (old_cameraid)
);

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

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

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

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

    -- modelnumberid
    c.modelid AS modelnumberid,

    -- machinetypeid = 32 (Camera)
    32 AS machinetypeid,

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

    -- serialnumber
    c.serialnumber,

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

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

    -- isactive
    c.isactive,

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

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

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

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

INSERT INTO temp_camera_mapping (old_cameraid, new_machineid)
SELECT
    c.cameraid,
    m.machineid
FROM cameras c
JOIN machines m ON m.serialnumber = c.serialnumber AND m.machinetypeid = 32
WHERE c.isactive = 1;

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

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

-- Count migrated cameras
SELECT 'Cameras in machines table:' AS status, COUNT(*) AS migrated_count
FROM machines
WHERE machinetypeid = 32;

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

-- Show mapping stats
SELECT 'ID mapping created:' AS status, COUNT(*) AS mapping_count
FROM temp_camera_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 04 completed successfully' AS status;
SELECT 'Next: Run 07_migrate_network_communications.sql' AS next_step;
SELECT 'Note: Skipping 05/06 if accesspoints/idfs tables do not exist' AS note;
SELECT '========================================' AS '';

SET SQL_SAFE_UPDATES = 1;

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