-- Create Sample Network Infrastructure Devices
-- Date: 2025-11-13
-- Purpose: Add test data for servers, switches, cameras, access points, and IDFs
-- Note: These devices go into the machines table with specific machinetypeid values

USE shopdb;

-- Network device type IDs:
-- 16 = Access Point
-- 17 = IDF
-- 18 = Camera
-- 19 = Switch
-- 20 = Server

-- Insert sample Switches (machinetypeid = 19)
INSERT INTO machines (machinenumber, machinetypeid, alias, mapleft, maptop, isactive, islocationonly)
VALUES
('SW-CORE-01', 19, 'Core Switch 1', 1200, 800, 1, 0),
('SW-DIST-01', 19, 'Distribution Switch 1', 1400, 900, 1, 0),
('SW-ACCESS-01', 19, 'Access Switch 1', 1600, 1000, 1, 0),
('SW-ACCESS-02', 19, 'Access Switch 2', 800, 1200, 1, 0),
('SW-OFFICE-01', 19, 'Office Switch', 1800, 1500, 1, 0);

-- Insert sample Servers (machinetypeid = 20)
INSERT INTO machines (machinenumber, machinetypeid, alias, mapleft, maptop, isactive, islocationonly)
VALUES
('SRV-DC-01', 20, 'Domain Controller 1', 1100, 700, 1, 0),
('SRV-SQL-01', 20, 'SQL Database Server', 1300, 750, 1, 0),
('SRV-FILE-01', 20, 'File Server', 1500, 800, 1, 0),
('SRV-WEB-01', 20, 'Web Application Server', 1700, 850, 1, 0),
('SRV-BACKUP-01', 20, 'Backup Server', 900, 650, 1, 0);

-- Insert sample Cameras (machinetypeid = 18)
INSERT INTO machines (machinenumber, machinetypeid, alias, mapleft, maptop, isactive, islocationonly)
VALUES
('CAM-ENTRY-01', 18, 'Main Entry Camera', 600, 1800, 1, 0),
('CAM-SHIPPING-01', 18, 'Shipping Dock Camera', 2000, 600, 1, 0),
('CAM-FLOOR-01', 18, 'Shop Floor Camera 1', 1500, 1200, 1, 0),
('CAM-FLOOR-02', 18, 'Shop Floor Camera 2', 1800, 1400, 1, 0),
('CAM-OFFICE-01', 18, 'Office Area Camera', 1200, 1900, 1, 0),
('CAM-PARKING-01', 18, 'Parking Lot Camera', 400, 2000, 1, 0);

-- Insert sample Access Points (machinetypeid = 16)
INSERT INTO machines (machinenumber, machinetypeid, alias, mapleft, maptop, isactive, islocationonly)
VALUES
('AP-OFFICE-01', 16, 'Office Access Point 1', 1100, 1800, 1, 0),
('AP-OFFICE-02', 16, 'Office Access Point 2', 1700, 1800, 1, 0),
('AP-SHOP-01', 16, 'Shop Floor AP 1', 1200, 1100, 1, 0),
('AP-SHOP-02', 16, 'Shop Floor AP 2', 1600, 1300, 1, 0),
('AP-WAREHOUSE-01', 16, 'Warehouse Access Point', 2100, 800, 1, 0);

-- Insert sample IDFs (machinetypeid = 17)
INSERT INTO machines (machinenumber, machinetypeid, alias, mapleft, maptop, isactive, islocationonly)
VALUES
('IDF-MAIN', 17, 'Main IDF Room', 1150, 750, 1, 0),
('IDF-EAST', 17, 'East Wing IDF', 1900, 1200, 1, 0),
('IDF-WEST', 17, 'West Wing IDF', 700, 1300, 1, 0),
('IDF-SHOP', 17, 'Shop Floor IDF', 1500, 1000, 1, 0);

-- Add IP addresses to some devices via communications table
-- Get the machineids we just created
SET @sw_core_id = (SELECT machineid FROM machines WHERE machinenumber = 'SW-CORE-01' LIMIT 1);
SET @srv_dc_id = (SELECT machineid FROM machines WHERE machinenumber = 'SRV-DC-01' LIMIT 1);
SET @srv_sql_id = (SELECT machineid FROM machines WHERE machinenumber = 'SRV-SQL-01' LIMIT 1);
SET @cam_entry_id = (SELECT machineid FROM machines WHERE machinenumber = 'CAM-ENTRY-01' LIMIT 1);
SET @ap_office_id = (SELECT machineid FROM machines WHERE machinenumber = 'AP-OFFICE-01' LIMIT 1);

-- Insert communications records (comstypeid = 1 for Ethernet)
INSERT INTO communications (machineid, comstypeid, address, isprimary, isactive)
VALUES
(@sw_core_id, 1, '10.80.1.1', 1, 1),
(@srv_dc_id, 1, '10.80.1.10', 1, 1),
(@srv_sql_id, 1, '10.80.1.11', 1, 1),
(@cam_entry_id, 1, '10.80.2.50', 1, 1),
(@ap_office_id, 1, '10.80.3.100', 1, 1);

-- Show summary
SELECT 'Sample Network Devices Created' AS status;

SELECT
    mt.machinetype,
    COUNT(*) AS total,
    SUM(CASE WHEN m.mapleft IS NOT NULL AND m.maptop IS NOT NULL THEN 1 ELSE 0 END) AS with_map_coords
FROM machines m
INNER JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE mt.machinetypeid IN (16, 17, 18, 19, 20)
GROUP BY mt.machinetype
ORDER BY mt.machinetypeid;
