-- =====================================================
-- SCRIPT 09: Update Views for Network Devices
-- =====================================================
-- Date: 2025-11-10
-- Purpose: Create/update views to include network 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: Create Unified Infrastructure View
-- =====================================================

SELECT '========================================' AS '';
SELECT 'Phase 3 Migration - Script 09' AS '';
SELECT 'Creating/Updating Views for Network Devices' AS '';
SELECT '========================================' AS '';

SELECT 'Creating vw_all_infrastructure view...' AS status;

-- Drop if exists
DROP VIEW IF EXISTS vw_all_infrastructure;

-- Create comprehensive view of all infrastructure
CREATE VIEW vw_all_infrastructure AS
SELECT
    m.machineid,
    m.machinenumber,
    m.alias,
    mt.machinetype,
    
    mo.modelnumber,
    v.vendor,
    m.serialnumber,
    c.address AS ipaddress,
    c.macaddress,
    m.mapleft,
    m.maptop,
    m.dateadded,
    m.lastupdated,
    CASE
        WHEN m.pctypeid IS NOT NULL THEN 'PC'
        WHEN mt.machinetypeid BETWEEN 30 AND 36 THEN 'Network Device'
        WHEN mt.machinetypeid BETWEEN 1 AND 24 THEN 'Equipment'
        ELSE 'Other'
    END AS device_category,
    m.isactive
FROM machines m
LEFT JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
LEFT JOIN communications c ON m.machineid = c.machineid AND c.isprimary = 1 AND c.comstypeid = 1
WHERE m.isactive = 1;

SELECT '✓ vw_all_infrastructure created' AS status;

-- =====================================================
-- STEP 2: Create Network Devices Summary View
-- =====================================================

SELECT 'Creating vw_network_devices_summary view...' AS status;

-- Drop if exists
DROP VIEW IF EXISTS vw_network_devices_summary;

-- Create network devices specific view
CREATE VIEW vw_network_devices_summary AS
SELECT
    m.machineid,
    m.machinenumber,
    m.alias,
    mt.machinetype,
    mo.modelnumber,
    v.vendor,
    m.serialnumber,
    c.address AS ipaddress,
    m.mapleft,
    m.maptop,
    -- Count relationships
    (SELECT COUNT(*)
     FROM machinerelationships mr
     WHERE mr.machineid = m.machineid AND mr.isactive = 1
    ) AS outgoing_relationships,
    (SELECT COUNT(*)
     FROM machinerelationships mr
     WHERE mr.related_machineid = m.machineid AND mr.isactive = 1
    ) AS incoming_relationships,
    m.isactive
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
LEFT JOIN communications c ON m.machineid = c.machineid AND c.isprimary = 1 AND c.comstypeid = 1
WHERE mt.machinetypeid BETWEEN 30 AND 36
AND m.isactive = 1;

SELECT '✓ vw_network_devices_summary created' AS status;

-- =====================================================
-- STEP 3: Create Network Topology View
-- =====================================================

SELECT 'Creating vw_network_topology view...' AS status;

-- Drop if exists
DROP VIEW IF EXISTS vw_network_topology;

-- Create topology view showing device connections
CREATE VIEW vw_network_topology AS
SELECT
    m1.machineid AS source_machineid,
    m1.machinenumber AS source_device,
    mt1.machinetype AS source_type,
    m2.machineid AS target_machineid,
    m2.machinenumber AS target_device,
    mt2.machinetype AS target_type,
    rt.relationshiptype,
    mr.relationship_notes,
    c1.address AS source_ip,
    c2.address AS target_ip
FROM machinerelationships mr
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
JOIN machines m1 ON mr.machineid = m1.machineid
JOIN machines m2 ON mr.related_machineid = m2.machineid
JOIN machinetypes mt1 ON m1.machinetypeid = mt1.machinetypeid
JOIN machinetypes mt2 ON m2.machinetypeid = mt2.machinetypeid
LEFT JOIN communications c1 ON m1.machineid = c1.machineid AND c1.isprimary = 1
LEFT JOIN communications c2 ON m2.machineid = c2.machineid AND c2.isprimary = 1
WHERE mr.isactive = 1
AND (mt1.machinetypeid BETWEEN 30 AND 36 OR mt2.machinetypeid BETWEEN 30 AND 36);

SELECT '✓ vw_network_topology created' AS status;

-- =====================================================
-- STEP 4: Verification - Show View Samples
-- =====================================================

SELECT '========================================' AS '';
SELECT 'View Verification:' AS '';
SELECT '========================================' AS '';

-- Sample from all infrastructure view
SELECT 'Sample from vw_all_infrastructure:' AS status;
SELECT
    machinenumber,
    machinetype,
    device_category,
    vendor,
    modelnumber,
    ipaddress
FROM vw_all_infrastructure
LIMIT 15;

-- Sample from network devices summary
SELECT 'Sample from vw_network_devices_summary:' AS status;
SELECT
    machinenumber,
    machinetype,
    vendor,
    ipaddress,
    outgoing_relationships,
    incoming_relationships
FROM vw_network_devices_summary
LIMIT 10;

-- Device category counts
SELECT 'Device counts by category:' AS status;
SELECT
    device_category,
    COUNT(*) AS device_count
FROM vw_all_infrastructure
GROUP BY device_category
ORDER BY device_count DESC;

-- Network device counts by type
SELECT 'Network devices by type:' AS status;
SELECT
    machinetype,
    COUNT(*) AS count
FROM vw_network_devices_summary
GROUP BY machinetype
ORDER BY count DESC;

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

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Views Created:
--
-- 1. vw_all_infrastructure
--    - Shows ALL machines (equipment, PCs, network devices)
--    - Includes device_category field for easy filtering
--    - Primary IP address included
--
-- 2. vw_network_devices_summary
--    - Network devices only (machinetypeid 30-36)
--    - Includes relationship counts
--    - Useful for network inventory
--
-- 3. vw_network_topology
--    - Shows all relationships involving network devices
--    - Includes both source and target device info
--    - Useful for network diagrams and topology mapping
--
-- Usage Examples:
--
-- All network devices:
-- SELECT * FROM vw_network_devices_summary;
--
-- All cameras with IPs:
-- SELECT * FROM vw_network_devices_summary WHERE machinetype = 'Camera';
--
-- Network connections:
-- SELECT * FROM vw_network_topology WHERE relationshiptype = 'Connected To';
--
-- All infrastructure by vendor:
-- SELECT vendor, device_category, COUNT(*) as count
-- FROM vw_all_infrastructure
-- GROUP BY vendor, device_category
-- ORDER BY vendor, device_category;
-- =====================================================
