-- =============================================================================
-- FIX: Migrate PCs from Old Machine Types to New Machine Types
-- =============================================================================
-- Date: 2025-11-21
-- Purpose: Fix PCs that were migrated to old machine type IDs (36-38) instead
--          of the new Phase 2 machine type IDs (33-35)
--
-- Issue: Production database had old PC machine types:
--   - ID 36: "PC - Standard" (should be ID 33: "Standard PC")
--   - ID 37: "PC - Shopfloor" (should be ID 35: "Shopfloor PC")
--   - ID 38: "PC - Engineer" (should be ID 34: "Engineering PC")
--
-- Impact: 212 of 286 PCs are using old machine type IDs
-- =============================================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

SELECT '============================================================' AS '';
SELECT 'FIX: MIGRATE PCS TO NEW MACHINE TYPE IDS' AS '';
SELECT '============================================================' AS '';
SELECT CONCAT('Start time: ', NOW()) AS '';
SELECT '' AS '';

-- =============================================================================
-- STEP 1: Check current distribution
-- =============================================================================

SELECT 'Current PC distribution across machine types:' AS '';
SELECT '' AS '';

SELECT
    mt.machinetypeid,
    mt.machinetype,
    CASE
        WHEN mt.machinetypeid IN (33, 34, 35) THEN 'NEW (Phase 2)'
        WHEN mt.machinetypeid IN (36, 37, 38) THEN 'OLD (Phase 1)'
        ELSE 'OTHER'
    END AS type_status,
    COUNT(*) as pc_count
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE m.pctypeid IS NOT NULL
GROUP BY mt.machinetypeid, mt.machinetype
ORDER BY mt.machinetypeid;

SELECT '' AS '';

-- =============================================================================
-- STEP 2: Migrate PCs to new machine type IDs
-- =============================================================================

SELECT 'Migrating PCs to new machine type IDs...' AS '';
SELECT '' AS '';

-- Migrate PC - Standard (36) to Standard PC (33)
UPDATE machines
SET machinetypeid = 33
WHERE machinetypeid = 36
  AND pctypeid IS NOT NULL;

SELECT CONCAT('✓ Migrated ', ROW_COUNT(), ' PCs from "PC - Standard" (36) to "Standard PC" (33)') AS status;

-- Migrate PC - Engineer (38) to Engineering PC (34)
UPDATE machines
SET machinetypeid = 34
WHERE machinetypeid = 38
  AND pctypeid IS NOT NULL;

SELECT CONCAT('✓ Migrated ', ROW_COUNT(), ' PCs from "PC - Engineer" (38) to "Engineering PC" (34)') AS status;

-- Migrate PC - Shopfloor (37) to Shopfloor PC (35)
UPDATE machines
SET machinetypeid = 35
WHERE machinetypeid = 37
  AND pctypeid IS NOT NULL;

SELECT CONCAT('✓ Migrated ', ROW_COUNT(), ' PCs from "PC - Shopfloor" (37) to "Shopfloor PC" (35)') AS status;

SELECT '' AS '';

-- =============================================================================
-- STEP 3: Verify migration
-- =============================================================================

SELECT 'Verifying migration...' AS '';
SELECT '' AS '';

SELECT 'PC distribution after migration:' AS '';
SELECT
    mt.machinetypeid,
    mt.machinetype,
    CASE
        WHEN mt.machinetypeid IN (33, 34, 35) THEN 'NEW (Phase 2)'
        WHEN mt.machinetypeid IN (36, 37, 38) THEN 'OLD (Phase 1)'
        ELSE 'OTHER'
    END AS type_status,
    COUNT(*) as pc_count
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE m.pctypeid IS NOT NULL
GROUP BY mt.machinetypeid, mt.machinetype
ORDER BY mt.machinetypeid;

SELECT '' AS '';

-- Check if any PCs still using old types
SET @old_type_count = (
    SELECT COUNT(*)
    FROM machines
    WHERE machinetypeid IN (36, 37, 38)
      AND pctypeid IS NOT NULL
);

SELECT CASE
    WHEN @old_type_count = 0 THEN '✓✓✓ ALL PCS MIGRATED TO NEW MACHINE TYPES ✓✓✓'
    ELSE CONCAT('⚠️ Still ', @old_type_count, ' PCs using old machine types!')
END AS status;

SELECT '' AS '';

-- =============================================================================
-- STEP 4: Summary by PC type
-- =============================================================================

SELECT 'Summary by PC type:' AS '';
SELECT
    pt.typename AS pctype,
    mt.machinetype,
    COUNT(*) as count
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
JOIN pctype pt ON m.pctypeid = pt.pctypeid
WHERE m.pctypeid IS NOT NULL
GROUP BY pt.typename, mt.machinetype
ORDER BY pt.typename, count DESC;

SELECT '' AS '';

-- =============================================================================
-- STEP 5: Deactivate old machine types (optional - can be done later)
-- =============================================================================

SELECT 'Old machine types status (not deactivated - can be done manually):' AS '';
SELECT machinetypeid, machinetype, isactive
FROM machinetypes
WHERE machinetypeid IN (36, 37, 38)
ORDER BY machinetypeid;

SELECT '' AS '';
SELECT '============================================================' AS '';
SELECT '✓✓✓ PC MACHINE TYPE MIGRATION COMPLETE ✓✓✓' AS '';
SELECT '============================================================' AS '';
SELECT '' AS '';

SELECT 'Final counts:' AS '';
SELECT CONCAT('Total PCs: ', COUNT(*)) AS summary FROM machines WHERE pctypeid IS NOT NULL
UNION ALL
SELECT CONCAT('Standard PCs (ID 33): ', COUNT(*)) FROM machines WHERE machinetypeid = 33 AND pctypeid IS NOT NULL
UNION ALL
SELECT CONCAT('Engineering PCs (ID 34): ', COUNT(*)) FROM machines WHERE machinetypeid = 34 AND pctypeid IS NOT NULL
UNION ALL
SELECT CONCAT('Shopfloor PCs (ID 35): ', COUNT(*)) FROM machines WHERE machinetypeid = 35 AND pctypeid IS NOT NULL;

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

SET SQL_SAFE_UPDATES = 1;

-- =============================================================================
-- NOTES
-- =============================================================================
--
-- Old machine types (36-38) can be deactivated or deleted after verification:
--   UPDATE machinetypes SET isactive = 0 WHERE machinetypeid IN (36, 37, 38);
--
-- Website queries should filter by pctypeid, not machinetypeid for PC types:
--   - All PCs: WHERE pctypeid IS NOT NULL
--   - Shopfloor PCs: WHERE pctypeid = (SELECT pctypeid FROM pctype WHERE typename = 'Shopfloor')
--   - Standard PCs: WHERE pctypeid = (SELECT pctypeid FROM pctype WHERE typename = 'Standard')
--   - Engineer PCs: WHERE pctypeid = (SELECT pctypeid FROM pctype WHERE typename = 'Engineer')
--
-- =============================================================================
