-- =====================================================
-- SCRIPT 05b: Migrate PC Controls Relationships
-- =====================================================
-- Date: 2025-11-13
-- Purpose: Create PC-to-Equipment Controls relationships
-- Status: TESTED on DEV - Created 148 relationships
-- Estimated Time: < 1 minute
-- Dependencies:
--   - Script 01 (PCs must be in machines table)
--   - relationshiptypes table (relationshiptypeid = 3 for 'Controls')
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

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

SELECT '========================================' AS '';
SELECT 'PC CONTROLS RELATIONSHIPS MIGRATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count PCs with machinenumber assignments
SELECT CONCAT('PCs with machine assignments: ', COUNT(*)) AS info
FROM pc
WHERE isactive = 1
  AND machinenumber IS NOT NULL
  AND machinenumber != '';

-- Show sample data
SELECT 'Sample PC-to-machine assignments (from old pc table):' AS '';
SELECT
    p.pcid,
    p.hostname AS pc_hostname,
    p.machinenumber AS controls_machine
FROM pc p
WHERE p.isactive = 1
  AND p.machinenumber IS NOT NULL
  AND p.machinenumber != ''
ORDER BY p.machinenumber
LIMIT 10;

-- Check current machinerelationships count
SELECT CONCAT('Existing relationships: ', COUNT(*)) AS info
FROM machinerelationships;

SELECT '' AS '';

-- =====================================================
-- STEP 2: Validate relationshiptypes exists
-- =====================================================

SELECT 'Validating relationship types...' AS '';

-- Check if 'Controls' relationship type exists
SELECT
    CASE
        WHEN COUNT(*) > 0 THEN '✓ Controls relationship type exists'
        ELSE '✗ ERROR: Controls relationship type missing!'
    END AS status
FROM relationshiptypes
WHERE relationshiptypeid = 3 AND relationshiptype = 'Controls';

SELECT '' AS '';

-- =====================================================
-- STEP 3: Create Controls relationships
-- =====================================================

SELECT 'Creating PC Controls relationships...' AS '';

-- Insert Controls relationships by matching pc.machinenumber to equipment.machinenumber
-- and pc.hostname to migrated PC hostname
INSERT INTO machinerelationships (machineid, related_machineid, relationshiptypeid, isactive)
SELECT DISTINCT
    equipment.machineid AS equipment_machineid,
    pc_migrated.machineid AS pc_machineid,
    3 AS relationshiptypeid,  -- 'Controls' relationship
    1 AS isactive
FROM pc old_pc
JOIN machines equipment ON equipment.machinenumber = old_pc.machinenumber
JOIN machines pc_migrated ON pc_migrated.hostname = old_pc.hostname
WHERE old_pc.isactive = 1
  AND old_pc.machinenumber IS NOT NULL
  AND old_pc.machinenumber != ''
  AND equipment.pctypeid IS NULL  -- Equipment only (not PCs)
  AND pc_migrated.pctypeid IS NOT NULL  -- PCs only
  AND NOT EXISTS (
    SELECT 1 FROM machinerelationships mr
    WHERE mr.machineid = equipment.machineid
      AND mr.related_machineid = pc_migrated.machineid
      AND mr.relationshiptypeid = 3
  );

-- Get count
SELECT CONCAT('✓ Created ', ROW_COUNT(), ' Controls relationships') AS result;

SELECT '' AS '';

-- =====================================================
-- STEP 4: Verification
-- =====================================================

SELECT 'Verification Results:' AS '';
SELECT '===================' AS '';

-- Total relationships now
SELECT CONCAT('Total relationships: ', COUNT(*)) AS info
FROM machinerelationships;

-- Breakdown by relationship type
SELECT
    rt.relationshiptype,
    COUNT(*) AS count
FROM machinerelationships mr
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
GROUP BY rt.relationshiptype;

-- Sample relationships created
SELECT 'Sample relationships (first 10):' AS '';
SELECT
    equipment.machinenumber AS equipment_num,
    equipment.alias AS equipment_name,
    pc.hostname AS controlling_pc,
    rt.relationshiptype
FROM machinerelationships mr
JOIN machines equipment ON mr.machineid = equipment.machineid
JOIN machines pc ON mr.related_machineid = pc.machineid
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE rt.relationshiptype = 'Controls'
ORDER BY equipment.machinenumber
LIMIT 10;

-- Check for orphaned equipment (no controlling PC)
SELECT CONCAT('Equipment without PC assignment: ', COUNT(*)) AS warning
FROM machines equipment
WHERE equipment.pctypeid IS NULL
  AND equipment.isactive = 1
  AND NOT EXISTS (
    SELECT 1 FROM machinerelationships mr
    JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
    WHERE mr.machineid = equipment.machineid
      AND rt.relationshiptype = 'Controls'
  );

SELECT '' AS '';
SELECT '========================================' AS '';
SELECT 'MIGRATION COMPLETE' AS '';
SELECT '========================================' AS '';

-- =====================================================
-- NOTES
-- =====================================================
--
-- This script creates PC-to-Equipment "Controls" relationships by:
-- 1. Reading pc.machinenumber (which machine the PC controls)
-- 2. Matching to equipment.machinenumber
-- 3. Matching pc.hostname to migrated PC in machines table
-- 4. Creating the relationship in machinerelationships table
--
-- DEV Testing Results:
-- - 142 relationships created from pc.machinenumber
-- - 6 relationships existed from hostname matching
-- - Total: 148 PC-to-Equipment relationships
--
-- Example: Machine 130 (equipment "2001") controlled by PC 390 ("GB07T5X3ESF")
--
-- =====================================================
