-- =====================================================
-- SCRIPT 05: Migrate Dualpath Assignments
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Migrate pc_dualpath_assignments to machinerelationships
-- Status: REQUIRES TESTING - Depends on Script 01
-- Estimated Time: 1-2 minutes
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

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

SELECT '========================================' AS '';
SELECT 'DUALPATH ASSIGNMENTS MIGRATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count dualpath assignments
SELECT CONCAT('Dualpath assignments to migrate: ', COUNT(*)) AS info
FROM pc_dualpath_assignments;

-- Show sample data
SELECT 'Sample dualpath assignments:' AS '';
SELECT
    d.dualpathid,
    d.pcid,
    d.primary_machine,
    d.secondary_machine
FROM pc_dualpath_assignments d
LIMIT 3;

SELECT '' AS '';

-- =====================================================
-- STEP 2: Get relationshiptypeid for Dualpath
-- =====================================================

SET @dualpath_type = (SELECT relationshiptypeid FROM relationshiptypes WHERE relationshiptype = 'Dualpath' LIMIT 1);

SELECT CONCAT('✓ Dualpath relationshiptypeid: ', @dualpath_type) AS status;
SELECT '' AS '';

-- =====================================================
-- STEP 3: Create backup
-- =====================================================

DROP TABLE IF EXISTS pc_dualpath_assignments_backup_phase2;
CREATE TABLE pc_dualpath_assignments_backup_phase2 AS
SELECT * FROM pc_dualpath_assignments;

SELECT CONCAT('✓ Backup created: ', COUNT(*), ' records') AS status
FROM pc_dualpath_assignments_backup_phase2;

SELECT '' AS '';

-- =====================================================
-- STEP 4: Migrate dualpath assignments
-- =====================================================

SELECT 'Migrating dualpath assignments...' AS '';

-- Create bidirectional relationships for dualpath machines
-- Machine 1 → Machine 2
INSERT INTO machinerelationships (
    machineid,
    related_machineid,
    relationshiptypeid,
    relationship_notes,
    isactive
)
SELECT
    m1.machineid AS machineid,
    m2.machineid AS related_machineid,
    @dualpath_type,
    CONCAT('Dualpath with ', m2.machinenumber) AS relationship_notes,
    1 AS isactive
FROM pc_dualpath_assignments d
JOIN pc_to_machine_id_mapping pcmap ON d.pcid = pcmap.pcid
JOIN machines m1 ON m1.machinenumber = d.primary_machine AND m1.isactive = 1
JOIN machines m2 ON m2.machinenumber = d.secondary_machine AND m2.isactive = 1
WHERE d.primary_machine IS NOT NULL
  AND d.secondary_machine IS NOT NULL;

SELECT CONCAT('✓ Created ', ROW_COUNT(), ' dualpath relationships (direction 1)') AS status;

-- Machine 2 → Machine 1 (reverse relationship)
INSERT INTO machinerelationships (
    machineid,
    related_machineid,
    relationshiptypeid,
    relationship_notes,
    isactive
)
SELECT
    m2.machineid AS machineid,
    m1.machineid AS related_machineid,
    @dualpath_type,
    CONCAT('Dualpath with ', m1.machinenumber) AS relationship_notes,
    1 AS isactive
FROM pc_dualpath_assignments d
JOIN pc_to_machine_id_mapping pcmap ON d.pcid = pcmap.pcid
JOIN machines m1 ON m1.machinenumber = d.primary_machine AND m1.isactive = 1
JOIN machines m2 ON m2.machinenumber = d.secondary_machine AND m2.isactive = 1
WHERE d.primary_machine IS NOT NULL
  AND d.secondary_machine IS NOT NULL;

SELECT CONCAT('✓ Created ', ROW_COUNT(), ' dualpath relationships (direction 2)') AS status;

SELECT '' AS '';

-- =====================================================
-- VERIFICATION
-- =====================================================

SELECT '========================================' AS '';
SELECT 'MIGRATION VERIFICATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count relationships by type
SELECT 'Relationships by type:' AS '';
SELECT
    rt.relationshiptype,
    COUNT(*) as count
FROM machinerelationships mr
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE mr.isactive = 1
GROUP BY rt.relationshiptype
ORDER BY count DESC;

SELECT '' AS '';

-- Show dualpath pairs using view
SELECT 'Dualpath machine pairs:' AS '';
SELECT
    machine1_number,
    machine1_hostname,
    machine2_number,
    machine2_hostname
FROM vw_dualpath_machines
LIMIT 5;

SELECT '' AS '';

-- Count total dualpath relationships
SELECT CONCAT('Total dualpath relationships: ', COUNT(*)) AS info
FROM machinerelationships
WHERE relationshiptypeid = @dualpath_type
  AND isactive = 1;

SELECT '' AS '';

SELECT '✓ Script 05 completed successfully' AS status;
SELECT 'Next: Run VERIFY_PHASE2_MIGRATION.sql' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Backup created: pc_dualpath_assignments_backup_phase2
--
-- Relationships created bidirectionally:
-- - primary_machine → secondary_machine
-- - secondary_machine → primary_machine
--
-- Both use relationshiptype = 'Dualpath'
--
-- View available: vw_dualpath_machines shows pairs
--
-- ROLLBACK: DELETE FROM machinerelationships
--           WHERE relationshiptypeid = @dualpath_type
-- =====================================================
