-- =====================================================
-- SCRIPT 01: Create Network Device Machine Types
-- =====================================================
-- Date: 2025-11-10
-- Purpose: Add machinetypes for network infrastructure devices
-- Part of: Phase 3 Migration (Network Devices → machines table)
-- Status: REVERSIBLE (see ROLLBACK script)
-- Estimated Time: < 1 minute
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Add Network Device Machine Types
-- =====================================================

SELECT '========================================' AS '';
SELECT 'Phase 3 Migration - Script 01' AS '';
SELECT 'Creating Network Device Machine Types' AS '';
SELECT '========================================' AS '';

-- Insert new machinetypes for network infrastructure
-- Skipping machinetypeid 30-36 if they already exist
INSERT INTO machinetypes (machinetypeid, machinetype, machinedescription, isactive)
VALUES
    (30, 'Server', 'Physical or virtual server', 1),
    (31, 'Switch', 'Network switch', 1),
    (32, 'Camera', 'IP camera or security camera', 1),
    (33, 'Access Point', 'Wireless access point', 1),
    (34, 'IDF', 'Intermediate Distribution Frame / Network closet', 1),
    (35, 'Router', 'Network router', 1),
    (36, 'Firewall', 'Network firewall / Security appliance', 1)
ON DUPLICATE KEY UPDATE
    machinetype = VALUES(machinetype),
    machinedescription = VALUES(machinedescription);

-- =====================================================
-- STEP 2: Verify Creation
-- =====================================================

SELECT '✓ Network device machinetypes created' AS status;

SELECT
    machinetypeid,
    machinetype,
    machinedescription,
    isactive
FROM machinetypes
WHERE machinetypeid BETWEEN 30 AND 36
ORDER BY machinetypeid;

-- =====================================================
-- STEP 3: Show Full Machine Type Structure
-- =====================================================

SELECT '========================================' AS '';
SELECT 'Current Machine Type Structure:' AS '';
SELECT '========================================' AS '';

SELECT
    CASE
        WHEN machinetypeid BETWEEN 1 AND 24 THEN '1. Equipment'
        WHEN machinetypeid BETWEEN 25 AND 29 THEN '2. PCs'
        WHEN machinetypeid BETWEEN 30 AND 36 THEN '3. Network Devices'
        ELSE '4. Other'
    END AS group_name,
    COUNT(*) AS type_count
FROM machinetypes
WHERE isactive = 1
GROUP BY group_name
ORDER BY group_name;

SELECT '========================================' AS '';
SELECT '✓ Script 01 completed successfully' AS status;
SELECT 'Next: Run 02_migrate_servers_to_machines.sql' AS next_step;
SELECT '========================================' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Machine Type ID Ranges:
-- 1-24:  Equipment (CNC machines, mills, lathes, etc.)
-- 25-29: PCs (Standard, Shopfloor, Engineer, Server, Laptop)
-- 30-36: Network Devices (Server, Switch, Camera, AP, IDF, Router, Firewall)
--
-- Filtering Examples:
-- - All equipment: WHERE pctypeid IS NULL AND machinetypeid BETWEEN 1 AND 24
-- - All PCs:       WHERE pctypeid IS NOT NULL
-- - All network:   WHERE pctypeid IS NULL AND machinetypeid BETWEEN 30 AND 36
-- - All servers:   WHERE machinetypeid = 30
-- =====================================================
