-- =============================================================================
-- Create/Update vw_network_devices View - Phase 2 Compatible
-- =============================================================================
-- Date: 2025-11-21
-- Purpose: Update vw_network_devices to query machines table for infrastructure
--          Printers remain in printers table (has fqdn column)
--          Other network devices (IDF, Server, Switch, Camera, Access Point) are in machines table
-- Machine Type IDs: 15=IDF, 16=Server, 17=Switch, 18=Camera, 19=Access Point, 20=Printer
-- =============================================================================

USE shopdb;

-- Drop existing view
DROP VIEW IF EXISTS vw_network_devices;

-- Create view with machines table for infrastructure + printers table for printers
CREATE VIEW vw_network_devices AS
-- Network infrastructure devices from machines table (IDF, Server, Switch, Camera, Access Point)
SELECT
    mt.machinetype AS device_type,
    m.machineid AS device_id,
    COALESCE(m.alias, m.machinenumber) AS device_name,
    m.modelnumberid AS modelid,
    mo.modelnumber,
    v.vendor,
    m.serialnumber,
    c.address AS ipaddress,
    NULL AS fqdn,  -- Infrastructure devices don't have FQDN
    m.machinenotes AS description,
    m.maptop,
    m.mapleft,
    m.isactive,
    -- Camera-specific: which IDF closet (from machinerelationships)
    (SELECT mr.related_machineid
     FROM machinerelationships mr
     JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
     WHERE mr.machineid = m.machineid
       AND rt.relationshiptype = 'Located In'
       AND mr.isactive = 1
     LIMIT 1) AS idfid,
    -- Camera-specific: IDF name
    (SELECT m2.machinenumber
     FROM machinerelationships mr
     JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
     JOIN machines m2 ON mr.related_machineid = m2.machineid
     WHERE mr.machineid = m.machineid
       AND rt.relationshiptype = 'Located In'
       AND mr.isactive = 1
     LIMIT 1) AS idfname,
    -- Camera-specific: MAC address (from communications table)
    (SELECT c2.macaddress
     FROM communications c2
     WHERE c2.machineid = m.machineid
       AND c2.macaddress IS NOT NULL
     LIMIT 1) AS macaddress
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.isactive = 1
WHERE m.machinetypeid IN (15, 16, 17, 18, 19)  -- IDF, Server, Switch, Camera, Access Point
  AND m.pctypeid IS NULL  -- Exclude PCs

UNION ALL

-- Printers from printers table (has fqdn column)
SELECT
    'Printer' AS device_type,
    p.printerid AS device_id,
    p.printerwindowsname AS device_name,
    p.modelid,
    m.modelnumber,
    v.vendor,
    p.serialnumber,
    p.ipaddress,
    p.fqdn,  -- Printers have FQDN
    NULL AS description,
    p.maptop,
    p.mapleft,
    p.isactive,
    NULL AS idfid,
    NULL AS idfname,
    NULL AS macaddress
FROM printers p
LEFT JOIN models m ON p.modelid = m.modelnumberid
LEFT JOIN vendors v ON m.vendorid = v.vendorid;

-- =============================================================================
-- Verification
-- =============================================================================

SELECT '✓ View created successfully (Phase 2)' AS status;

SELECT 'View columns:' AS '';
SELECT COLUMN_NAME, ORDINAL_POSITION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'vw_network_devices'
ORDER BY ORDINAL_POSITION;

SELECT '' AS '';
SELECT 'Device counts by type:' AS '';
SELECT device_type, COUNT(*) AS count
FROM vw_network_devices
GROUP BY device_type
ORDER BY device_type;

SELECT '' AS '';
SELECT 'Sample devices (first 10):' AS '';
SELECT device_type, device_name, vendor, modelnumber, ipaddress, fqdn
FROM vw_network_devices
LIMIT 10;

-- =============================================================================
-- NOTES
-- =============================================================================
--
-- View Columns (16 total):
-- 1.  device_type      - Type: IDF, Server, Switch, Camera, Access Point (from machines), Printer (from printers)
-- 2.  device_id        - machineid or printerid
-- 3.  device_name      - Device name/alias
-- 4.  modelid          - Foreign key to models table
-- 5.  modelnumber      - Model number from models table
-- 6.  vendor           - Vendor name from vendors table
-- 7.  serialnumber     - Serial number
-- 8.  ipaddress        - IP address
-- 9.  fqdn             - FQDN (Printers only)
-- 10. description      - Description
-- 11. maptop           - Y coordinate for map display
-- 12. mapleft          - X coordinate for map display
-- 13. isactive         - Active flag
-- 14. idfid            - IDF ID (Camera only, from machinerelationships)
-- 15. idfname          - IDF name (Camera only, from machinerelationships)
-- 16. macaddress       - MAC address (Camera only, from communications)
--
-- Machine Type IDs:
-- 15 = IDF
-- 16 = Server
-- 17 = Switch
-- 18 = Camera
-- 19 = Access Point
-- 20 = Printer (still in printers table, not machines)
--
-- Data Sources:
-- - IDFs, Servers, Switches, Cameras, Access Points: machines table
-- - Printers: printers table (has fqdn column)
--
-- Used by: network_devices.asp
--
-- To add a new IDF:
-- INSERT INTO machines (machinetypeid, machinenumber, alias, mapleft, maptop, isactive)
-- VALUES (15, 'IDF-NAME', 'Description', 100, 100, 1);
--
-- =============================================================================
