-- =============================================================================
-- PHASE 1.0: Ensure All Machine Types Match Dev EXACTLY
-- =============================================================================
-- Date: 2025-11-21
-- Purpose: Ensure production has EXACT same machine types as dev database
--          This is CRITICAL because website code uses specific machinetypeid values
--
-- IMPORTANT: This script uses INSERT IGNORE with specific IDs to match dev
--
-- Dev Machine Types:
--   1-14:  Equipment (existing in production)
--   15-20: Infrastructure (MUST ADD with exact IDs and colors)
--   21-25: Equipment (existing in production)
--   33-35: PC types (MUST ADD with exact IDs)
--
-- Run this as the FIRST step in Phase 1 migration
-- Estimated Time: 30 seconds
-- =============================================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

SELECT '============================================================' AS '';
SELECT 'PHASE 1.0: ENSURE MACHINE TYPES MATCH DEV EXACTLY' AS '';
SELECT '============================================================' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

-- =============================================================================
-- STEP 1: Check current machine types
-- =============================================================================

SELECT 'Current production machine types:' AS '';
SELECT COUNT(*) AS total_machine_types FROM machinetypes;
SELECT '' AS '';

SELECT 'Existing machine types in production:' AS '';
SELECT machinetypeid, machinetype, bgcolor, machinedescription
FROM machinetypes
ORDER BY machinetypeid;

SELECT '' AS '';

-- =============================================================================
-- STEP 2: Check for missing machine types
-- =============================================================================

SELECT 'Checking for missing machine types...' AS '';

SET @missing_15 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 15);
SET @missing_16 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 16);
SET @missing_17 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 17);
SET @missing_18 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 18);
SET @missing_19 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 19);
SET @missing_20 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 20);
SET @missing_33 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 33);
SET @missing_34 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 34);
SET @missing_35 = NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 35);

SELECT
    CASE WHEN @missing_15 THEN '⚠️ Missing ID 15 (Printer)' ELSE '✓ Has ID 15 (Printer)' END AS check_15,
    CASE WHEN @missing_16 THEN '⚠️ Missing ID 16 (Access Point)' ELSE '✓ Has ID 16 (Access Point)' END AS check_16,
    CASE WHEN @missing_17 THEN '⚠️ Missing ID 17 (IDF)' ELSE '✓ Has ID 17 (IDF)' END AS check_17,
    CASE WHEN @missing_18 THEN '⚠️ Missing ID 18 (Camera)' ELSE '✓ Has ID 18 (Camera)' END AS check_18,
    CASE WHEN @missing_19 THEN '⚠️ Missing ID 19 (Switch)' ELSE '✓ Has ID 19 (Switch)' END AS check_19,
    CASE WHEN @missing_20 THEN '⚠️ Missing ID 20 (Server)' ELSE '✓ Has ID 20 (Server)' END AS check_20,
    CASE WHEN @missing_33 THEN '⚠️ Missing ID 33 (Standard PC)' ELSE '✓ Has ID 33 (Standard PC)' END AS check_33,
    CASE WHEN @missing_34 THEN '⚠️ Missing ID 34 (Engineering PC)' ELSE '✓ Has ID 34 (Engineering PC)' END AS check_34,
    CASE WHEN @missing_35 THEN '⚠️ Missing ID 35 (Shopfloor PC)' ELSE '✓ Has ID 35 (Shopfloor PC)' END AS check_35;

SELECT '' AS '';

-- =============================================================================
-- STEP 3: Add infrastructure machine types (EXACT match to dev)
-- =============================================================================

SELECT 'Adding infrastructure machine types to match dev...' AS '';
SELECT '' AS '';

-- ID 15: Printer (with bgcolor #4CAF50 - green)
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    15,
    'Printer',
    b'1',
    1,
    '#4CAF50',
    'Network printer - HP, Xerox, or other print devices',
    NULL
);

-- ID 16: Access Point (with bgcolor #2196F3 - blue)
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    16,
    'Access Point',
    b'1',
    1,
    '#2196F3',
    'Wireless access point for network connectivity',
    NULL
);

-- ID 17: IDF (with bgcolor #FF9800 - orange)
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    17,
    'IDF',
    b'1',
    1,
    '#FF9800',
    'Intermediate Distribution Frame - network equipment closet',
    NULL
);

-- ID 18: Camera (with bgcolor #F44336 - red)
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    18,
    'Camera',
    b'1',
    1,
    '#F44336',
    'Security camera for facility monitoring',
    NULL
);

-- ID 19: Switch (with bgcolor #9C27B0 - purple)
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    19,
    'Switch',
    b'1',
    1,
    '#9C27B0',
    'Network switch for connectivity',
    NULL
);

-- ID 20: Server (with bgcolor #607D8B - blue-gray)
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    20,
    'Server',
    b'1',
    1,
    '#607D8B',
    'Physical or virtual server',
    NULL
);

SELECT '✓ Infrastructure machine types added (IDs 15-20)' AS status;
SELECT '' AS '';

-- =============================================================================
-- STEP 4: Deactivate old PC machine types if they exist
-- =============================================================================

SELECT 'Checking for old PC machine types (36-38)...' AS '';
SELECT '' AS '';

-- Deactivate old machine types to prevent confusion
UPDATE machinetypes
SET isactive = b'0'
WHERE machinetypeid IN (36, 37, 38);

SELECT CONCAT('✓ Deactivated ', ROW_COUNT(), ' old PC machine types (if they existed)') AS status;
SELECT '' AS '';

-- =============================================================================
-- STEP 5: Add PC machine types (EXACT match to dev)
-- =============================================================================

SELECT 'Adding PC machine types to match dev...' AS '';
SELECT '' AS '';

-- ID 33: Standard PC
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    33,
    'Standard PC',
    b'1',
    1,
    NULL,
    'Standard user workstation computer',
    NULL
);

-- ID 34: Engineering PC
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    34,
    'Engineering PC',
    b'1',
    1,
    NULL,
    'Engineering workstation with specialized software',
    NULL
);

-- ID 35: Shopfloor PC
INSERT IGNORE INTO machinetypes (
    machinetypeid,
    machinetype,
    isactive,
    functionalaccountid,
    bgcolor,
    machinedescription,
    builddocurl
)
VALUES (
    35,
    'Shopfloor PC',
    b'1',
    1,
    NULL,
    'Shop floor computer for machine monitoring and control',
    NULL
);

SELECT '✓ PC machine types added (IDs 33-35)' AS status;
SELECT '' AS '';

-- =============================================================================
-- STEP 6: Verification - Compare with Dev
-- =============================================================================

SELECT 'Verifying production matches dev...' AS '';
SELECT '' AS '';

-- Check all required types exist
SELECT 'Required machine types verification:' AS '';
SELECT
    'ID' AS id,
    'Type' AS type,
    'Status' AS status,
    'Bgcolor' AS bgcolor
UNION ALL
SELECT
    '15',
    'Printer',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 15) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 15)
UNION ALL
SELECT
    '16',
    'Access Point',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 16) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 16)
UNION ALL
SELECT
    '17',
    'IDF',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 17) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 17)
UNION ALL
SELECT
    '18',
    'Camera',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 18) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 18)
UNION ALL
SELECT
    '19',
    'Switch',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 19) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 19)
UNION ALL
SELECT
    '20',
    'Server',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 20) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 20)
UNION ALL
SELECT
    '33',
    'Standard PC',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 33) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 33)
UNION ALL
SELECT
    '34',
    'Engineering PC',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 34) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 34)
UNION ALL
SELECT
    '35',
    'Shopfloor PC',
    CASE WHEN EXISTS (SELECT 1 FROM machinetypes WHERE machinetypeid = 35) THEN '✓' ELSE '✗' END,
    (SELECT bgcolor FROM machinetypes WHERE machinetypeid = 35);

SELECT '' AS '';

-- Show all machine types after additions
SELECT 'All machine types in production (after sync):' AS '';
SELECT machinetypeid, machinetype, isactive, bgcolor, machinedescription
FROM machinetypes
ORDER BY machinetypeid;

SELECT '' AS '';

-- Count by category
SELECT 'Machine types by category:' AS '';
SELECT
    CASE
        WHEN machinetypeid IN (33, 34, 35) THEN 'PC Types'
        WHEN machinetypeid IN (15, 16, 17, 18, 19, 20) THEN 'Infrastructure'
        ELSE 'Equipment'
    END AS category,
    COUNT(*) AS count,
    GROUP_CONCAT(CONCAT(machinetypeid, ':', machinetype) ORDER BY machinetypeid SEPARATOR ', ') AS types
FROM machinetypes
WHERE isactive = b'1'
GROUP BY category;

SELECT '' AS '';

-- =============================================================================
-- STEP 7: Final summary
-- =============================================================================

SELECT '============================================================' AS '';
SELECT '✓✓✓ MACHINE TYPES NOW MATCH DEV EXACTLY ✓✓✓' AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

SELECT 'Machine types summary:' AS '';
SELECT CONCAT('Total machine types: ', COUNT(*)) AS summary FROM machinetypes;
SELECT CONCAT('Active machine types: ', COUNT(*)) AS summary FROM machinetypes WHERE isactive = b'1';
SELECT CONCAT('Infrastructure types (15-20): ', COUNT(*)) AS summary FROM machinetypes WHERE machinetypeid BETWEEN 15 AND 20;
SELECT CONCAT('PC types (33-35): ', COUNT(*)) AS summary FROM machinetypes WHERE machinetypeid BETWEEN 33 AND 35;

SELECT '' AS '';

SELECT 'Website color coding preserved:' AS '';
SELECT '  - Printer (15): Green #4CAF50' AS '';
SELECT '  - Access Point (16): Blue #2196F3' AS '';
SELECT '  - IDF (17): Orange #FF9800' AS '';
SELECT '  - Camera (18): Red #F44336' AS '';
SELECT '  - Switch (19): Purple #9C27B0' AS '';
SELECT '  - Server (20): Blue-Gray #607D8B' AS '';

SELECT '' AS '';

SELECT 'Differentiation now works:' AS '';
SELECT '  - PCs: WHERE pctypeid IS NOT NULL' AS '';
SELECT '  - Equipment: WHERE pctypeid IS NULL AND machinetypeid NOT IN (15-20)' AS '';
SELECT '  - Infrastructure: WHERE pctypeid IS NULL AND machinetypeid IN (15-20)' AS '';

SELECT '' AS '';
SELECT CONCAT('Completed at: ', NOW()) AS '';
SELECT '============================================================' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =============================================================================
-- EXPECTED DEV MACHINE TYPES (for reference)
-- =============================================================================
--
-- machinetypeid | machinetype               | bgcolor   | Description
-- --------------|---------------------------|-----------|---------------------------
-- 1             | LocationOnly              | #ffffff   | NULL
-- 2             | Vertical Lathe            | #ffffff   | NULL
-- 3             | CMM                       | #ffffff   | Coordinate-measuring machine
-- 4             | Lathe                     | #ffffff   | Okuma & Howa 2SPV80...
-- 5             | Wax Trace                 | #ffffff   | NULL
-- 6             | Mill Turn                 | #ffffff   |
-- 7             | Intertia Welder           | #ffffff   | NULL
-- 8             | Eddy Current              | #ffffff   | Wild Stallions...
-- 9             | Shotpeen                  | #ffffff   | Shot peening...
-- 10            | Part Washer               | #ffffff   | NULL
-- 11            | Grinder                   | NULL      | NULL
-- 12            | Broach                    | NULL      | NULL
-- 13            | 5-axis Mill               | NULL      |
-- 14            | Furnace                   | NULL      |
-- 15            | Printer                   | #4CAF50   | Network printer... ✨ ADDED
-- 16            | Access Point              | #2196F3   | Wireless access... ✨ ADDED
-- 17            | IDF                       | #FF9800   | Intermediate... ✨ ADDED
-- 18            | Camera                    | #F44336   | Security camera... ✨ ADDED
-- 19            | Switch                    | #9C27B0   | Network switch... ✨ ADDED
-- 20            | Server                    | #607D8B   | Physical or virtual... ✨ ADDED
-- 21            | Hobbing Machine           | NULL      | NULL
-- 22            | Robotic Deburring         | NULL      |
-- 23            | Measuring Machine         | NULL      | NULL
-- 24            | Vertical Turning Center   | NULL      | NULL
-- 25            | Horizontal Machining Ctr  | NULL      | NULL
-- 33            | Standard PC               | NULL      | Standard user... ✨ ADDED
-- 34            | Engineering PC            | NULL      | Engineering... ✨ ADDED
-- 35            | Shopfloor PC              | NULL      | Shop floor computer... ✨ ADDED
--
-- =============================================================================
