-- =====================================================
-- SCRIPT 08: Create Network Relationship Types
-- =====================================================
-- Date: 2025-11-10
-- Purpose: Add new relationship types for network infrastructure
-- 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: Show Existing Relationship Types
-- =====================================================

SELECT '========================================' AS '';
SELECT 'Phase 3 Migration - Script 08' AS '';
SELECT 'Creating Network Relationship Types' AS '';
SELECT '========================================' AS '';

SELECT 'Existing relationship types:' AS status;

SELECT
    relationshiptypeid,
    relationshiptype,
    description,
    displayorder
FROM relationshiptypes
WHERE isactive = 1
ORDER BY relationshiptypeid;

-- =====================================================
-- STEP 2: Add New Network Relationship Types
-- =====================================================

SELECT 'Adding new network relationship types...' AS status;

-- Insert new relationship types
-- Using explicit IDs to maintain consistency
INSERT INTO relationshiptypes (relationshiptype, description, isactive)
VALUES
    ('Connected To', 'Device physically connected to network infrastructure', 1),
    ('Powered By', 'Device powered by this power source', 1),
    ('Mounted In', 'Device mounted in this rack or cabinet', 1),
    ('Feeds Video To', 'Camera feeds video stream to this server/NVR', 1),
    ('Provides Network', 'Infrastructure provides network connectivity to device', 1)
ON DUPLICATE KEY UPDATE
    description = VALUES(description);

-- =====================================================
-- STEP 3: Verification
-- =====================================================

SELECT '========================================' AS '';
SELECT 'Relationship Types After Migration:' AS '';
SELECT '========================================' AS '';

SELECT
    relationshiptypeid,
    relationshiptype,
    description,
    displayorder,
    CASE
        WHEN relationshiptypeid <= 6 THEN 'Legacy'
        ELSE 'Network (New)'
    END AS category
FROM relationshiptypes
WHERE isactive = 1
ORDER BY relationshiptypeid;

-- Show counts
SELECT 'Total relationship types:' AS status, COUNT(*) AS total_count
FROM relationshiptypes
WHERE isactive = 1;

SELECT '========================================' AS '';
SELECT '✓ Script 08 completed successfully' AS status;
SELECT 'Next: Run 09_update_views_for_network_devices.sql' AS next_step;
SELECT '========================================' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- New Relationship Types Added:
--
-- 1. Connected To (directional)
--    - Use for: Camera → Switch, Switch → IDF, etc.
--    - Example: CAM-0001 is Connected To SW-Core-01
--
-- 2. Powered By (directional)
--    - Use for: Device → Power Source
--    - Example: SW-Floor-01 Powered By UPS-Room-A
--
-- 3. Mounted In (directional)
--    - Use for: Device → Rack/Cabinet
--    - Example: SVR-0001 Mounted In Rack-A-01
--
-- 4. Feeds Video To (directional)
--    - Use for: Camera → Server/NVR
--    - Example: CAM-Shop-01 Feeds Video To SVR-Security-01
--
-- 5. Provides Network (directional)
--    - Use for: Infrastructure → Device
--    - Example: IDF-Building-A Provides Network to SW-Floor-02
--
-- Usage Examples:
--
-- Camera connected to switch:
-- INSERT INTO machinerelationships (machineid, related_machineid, relationshiptypeid)
-- SELECT
--     (SELECT machineid FROM machines WHERE machinenumber = 'CAM-Shop-01'),
--     (SELECT machineid FROM machines WHERE machinenumber = 'SW-Core-01'),
--     (SELECT relationshiptypeid FROM relationshiptypes WHERE relationshiptype = 'Connected To');
--
-- Switch connected to IDF:
-- INSERT INTO machinerelationships (machineid, related_machineid, relationshiptypeid)
-- SELECT
--     (SELECT machineid FROM machines WHERE machinenumber = 'SW-Core-01'),
--     (SELECT machineid FROM machines WHERE machinenumber = 'IDF-Building-A'),
--     (SELECT relationshiptypeid FROM relationshiptypes WHERE relationshiptype = 'Connected To');
-- =====================================================
