-- dev_seed.sql (SQLite) — development test data
--
-- Running this script is idempotent: existing rows are left untouched
-- and missing rows are inserted.  Safe to run multiple times.
--
-- Load with: cargo run --features ssr --bin seed

-- ── Networks ──────────────────────────────────────────────────────────────────
-- INSERT OR IGNORE relies on the UNIQUE constraint on cidr.

INSERT OR IGNORE INTO networks (cidr) VALUES ('192.168.1.0/24');
INSERT OR IGNORE INTO networks (cidr) VALUES ('192.168.10.0/24');
INSERT OR IGNORE INTO networks (cidr) VALUES ('10.0.0.0/8');
INSERT OR IGNORE INTO networks (cidr) VALUES ('172.16.0.0/16');

-- ── Hosts ─────────────────────────────────────────────────────────────────────
-- Hosts have no UNIQUE constraint, so we guard each insert with WHERE NOT EXISTS.
-- Network IDs are resolved by subquery on cidr for portability.

-- LAN — 192.168.1.0/24
INSERT INTO hosts (name, ip, network_id) SELECT 'gateway',        '192.168.1.1',   id FROM networks WHERE cidr = '192.168.1.0/24'  AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'gateway'        AND ip = '192.168.1.1');
INSERT INTO hosts (name, ip, network_id) SELECT 'workstation-01', '192.168.1.10',  id FROM networks WHERE cidr = '192.168.1.0/24'  AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'workstation-01' AND ip = '192.168.1.10');
INSERT INTO hosts (name, ip, network_id) SELECT 'workstation-02', '192.168.1.11',  id FROM networks WHERE cidr = '192.168.1.0/24'  AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'workstation-02' AND ip = '192.168.1.11');
INSERT INTO hosts (name, ip, network_id) SELECT 'workstation-03', '192.168.1.12',  id FROM networks WHERE cidr = '192.168.1.0/24'  AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'workstation-03' AND ip = '192.168.1.12');
INSERT INTO hosts (name, ip, network_id) SELECT 'nas-01',         '192.168.1.20',  id FROM networks WHERE cidr = '192.168.1.0/24'  AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'nas-01'         AND ip = '192.168.1.20');
INSERT INTO hosts (name, ip, network_id) SELECT 'printer-01',     '192.168.1.50',  id FROM networks WHERE cidr = '192.168.1.0/24'  AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'printer-01'     AND ip = '192.168.1.50');

-- DMZ — 192.168.10.0/24
INSERT INTO hosts (name, ip, network_id) SELECT 'web-server-01',  '192.168.10.10', id FROM networks WHERE cidr = '192.168.10.0/24' AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'web-server-01'  AND ip = '192.168.10.10');
INSERT INTO hosts (name, ip, network_id) SELECT 'web-server-02',  '192.168.10.11', id FROM networks WHERE cidr = '192.168.10.0/24' AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'web-server-02'  AND ip = '192.168.10.11');
INSERT INTO hosts (name, ip, network_id) SELECT 'db-server-01',   '192.168.10.20', id FROM networks WHERE cidr = '192.168.10.0/24' AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'db-server-01'   AND ip = '192.168.10.20');
INSERT INTO hosts (name, ip, network_id) SELECT 'mail-server-01', '192.168.10.30', id FROM networks WHERE cidr = '192.168.10.0/24' AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'mail-server-01' AND ip = '192.168.10.30');

-- Corporate backbone — 10.0.0.0/8
INSERT INTO hosts (name, ip, network_id) SELECT 'core-switch-01',   '10.0.0.1',   id FROM networks WHERE cidr = '10.0.0.0/8'       AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'core-switch-01'   AND ip = '10.0.0.1');
INSERT INTO hosts (name, ip, network_id) SELECT 'monitoring-01',    '10.0.1.10',  id FROM networks WHERE cidr = '10.0.0.0/8'       AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'monitoring-01'    AND ip = '10.0.1.10');
INSERT INTO hosts (name, ip, network_id) SELECT 'backup-server-01', '10.0.1.20',  id FROM networks WHERE cidr = '10.0.0.0/8'       AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'backup-server-01' AND ip = '10.0.1.20');
INSERT INTO hosts (name, ip, network_id) SELECT 'log-server-01',    '10.0.1.30',  id FROM networks WHERE cidr = '10.0.0.0/8'       AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'log-server-01'    AND ip = '10.0.1.30');

-- VPN — 172.16.0.0/16
INSERT INTO hosts (name, ip, network_id) SELECT 'vpn-gateway-01', '172.16.0.1',  id FROM networks WHERE cidr = '172.16.0.0/16'    AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'vpn-gateway-01' AND ip = '172.16.0.1');
INSERT INTO hosts (name, ip, network_id) SELECT 'vpn-client-01',  '172.16.1.10', id FROM networks WHERE cidr = '172.16.0.0/16'    AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'vpn-client-01'  AND ip = '172.16.1.10');
INSERT INTO hosts (name, ip, network_id) SELECT 'vpn-client-02',  '172.16.1.11', id FROM networks WHERE cidr = '172.16.0.0/16'    AND NOT EXISTS (SELECT 1 FROM hosts WHERE name = 'vpn-client-02'  AND ip = '172.16.1.11');
