-- mCables v4 device-type catalog. See docs/design.md §2.1 + §2.2.

-- v4 — device-type catalog. Built-in types live globally (project_id NULL).
-- Per-project custom types use project_id = X.
CREATE TABLE device_types (
    id              INTEGER PRIMARY KEY,
    project_id      INTEGER REFERENCES projects(id) ON DELETE CASCADE,
    name            TEXT NOT NULL,
    kind            TEXT NOT NULL DEFAULT 'generic',
    icon            TEXT,
    description     TEXT NOT NULL DEFAULT '',
    built_in        INTEGER NOT NULL DEFAULT 0,
    created_at      TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at      TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (project_id, name)
);
CREATE INDEX device_types_project_idx ON device_types(project_id);

-- v4 — port profile per device type. Used to seed ports when a device
-- of that type is created.
CREATE TABLE device_type_ports (
    id              INTEGER PRIMARY KEY,
    device_type_id  INTEGER NOT NULL REFERENCES device_types(id) ON DELETE CASCADE,
    cable_type_id   INTEGER NOT NULL REFERENCES cable_types(id)  ON DELETE RESTRICT,
    label_prefix    TEXT NOT NULL DEFAULT '',
    count           INTEGER NOT NULL DEFAULT 1 CHECK (count >= 1),
    edge            TEXT NOT NULL DEFAULT 'bottom' CHECK (edge IN ('top','bottom','left','right')),
    sort_order      INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX device_type_ports_type_idx ON device_type_ports(device_type_id);

-- v4 — devices gain a nullable type_id. SET NULL on type-delete so we
-- never cascade-delete a device the user still wants.
ALTER TABLE devices ADD COLUMN type_id INTEGER
    REFERENCES device_types(id) ON DELETE SET NULL;
CREATE INDEX devices_type_idx ON devices(type_id);

-- Seed the 14 built-in device types.
-- project_id stays NULL → built-in. The trio Screen / Keyboard / Mouse
-- was added in v4.1 to support the Home Office setup template (slice 6).
INSERT INTO device_types (name, kind, built_in, description) VALUES
    ('NAS',        'storage',   1, 'Network-attached storage'),
    ('PC',         'compute',   1, 'Desktop PC / workstation'),
    ('Mac',        'compute',   1, 'Mac (mini / studio / desktop)'),
    ('Notebook',   'compute',   1, 'Laptop / notebook'),
    ('TV',         'display',   1, 'Television'),
    ('Soundbar',   'audio',     1, 'Soundbar / AV receiver'),
    ('Switch',     'network',   1, 'Ethernet switch'),
    ('fritz',      'network',   1, 'AVM Fritz!Box router'),
    ('ChromeCast', 'display',   1, 'ChromeCast / streaming stick'),
    ('SteamLink',  'compute',   1, 'Steam Link / dedicated streaming box'),
    ('IOx-3',      'hub',       1, 'USB hub with 3 downstream ports'),
    ('IOx-6',      'hub',       1, 'USB hub with 6 downstream ports'),
    ('IOx-8',      'hub',       1, 'USB hub with 8 downstream ports'),
    ('Screen',     'display',   1, 'External monitor / display'),
    ('Keyboard',   'accessory', 1, 'Keyboard'),
    ('Mouse',      'accessory', 1, 'Mouse / pointing device');

-- Now seed device_type_ports. Each row references its device_type by
-- (SELECT id FROM device_types WHERE name = ? AND project_id IS NULL).
--
-- cable_types ids come from the 001 seed in fixed order:
--   1=Power, 2=USB, 3=HDMI, 4=DP, 5=RJ45
--
-- label_prefix is what the seeder appends a 1..N suffix to when count>1.
-- Default edge is 'bottom'; sort_order positions the port-types from
-- left to right along that edge.

-- NAS: Power × 1, RJ45 × 1
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='NAS' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 5, 'RJ45',  1, 'bottom', 1 FROM device_types WHERE name='NAS' AND project_id IS NULL;

-- PC: Power × 1, RJ45 × 1, HDMI × 1, USB × 2
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='PC' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 5, 'RJ45',  1, 'bottom', 1 FROM device_types WHERE name='PC' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 3, 'HDMI',  1, 'bottom', 2 FROM device_types WHERE name='PC' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   2, 'bottom', 3 FROM device_types WHERE name='PC' AND project_id IS NULL;

-- Mac: Power × 1, HDMI × 1, USB × 2
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='Mac' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 3, 'HDMI',  1, 'bottom', 1 FROM device_types WHERE name='Mac' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   2, 'bottom', 2 FROM device_types WHERE name='Mac' AND project_id IS NULL;

-- Notebook: Power × 1, USB × 2
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='Notebook' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   2, 'bottom', 1 FROM device_types WHERE name='Notebook' AND project_id IS NULL;

-- TV: Power × 1, HDMI × 2
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='TV' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 3, 'HDMI',  2, 'bottom', 1 FROM device_types WHERE name='TV' AND project_id IS NULL;

-- Soundbar: Power × 1, HDMI × 1
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='Soundbar' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 3, 'HDMI',  1, 'bottom', 1 FROM device_types WHERE name='Soundbar' AND project_id IS NULL;

-- Switch: Power × 1, RJ45 × 5
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='Switch' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 5, 'RJ45',  5, 'bottom', 1 FROM device_types WHERE name='Switch' AND project_id IS NULL;

-- fritz: Power × 1, RJ45 × 4
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='fritz' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 5, 'RJ45',  4, 'bottom', 1 FROM device_types WHERE name='fritz' AND project_id IS NULL;

-- ChromeCast: Power × 1, HDMI × 1
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='ChromeCast' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 3, 'HDMI',  1, 'bottom', 1 FROM device_types WHERE name='ChromeCast' AND project_id IS NULL;

-- SteamLink: Power × 1, HDMI × 1, USB × 2
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='SteamLink' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 3, 'HDMI',  1, 'bottom', 1 FROM device_types WHERE name='SteamLink' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   2, 'bottom', 2 FROM device_types WHERE name='SteamLink' AND project_id IS NULL;

-- IOx-3: Power × 1, USB × 3
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='IOx-3' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   3, 'bottom', 1 FROM device_types WHERE name='IOx-3' AND project_id IS NULL;

-- IOx-6: Power × 1, USB × 6
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='IOx-6' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   6, 'bottom', 1 FROM device_types WHERE name='IOx-6' AND project_id IS NULL;

-- IOx-8: Power × 1, USB × 8
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='IOx-8' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   8, 'bottom', 1 FROM device_types WHERE name='IOx-8' AND project_id IS NULL;

-- Screen: Power × 1, HDMI × 1 (v4.1)
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 1, 'Power', 1, 'bottom', 0 FROM device_types WHERE name='Screen' AND project_id IS NULL;
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 3, 'HDMI',  1, 'bottom', 1 FROM device_types WHERE name='Screen' AND project_id IS NULL;

-- Keyboard: USB × 1 (v4.1)
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   1, 'bottom', 0 FROM device_types WHERE name='Keyboard' AND project_id IS NULL;

-- Mouse: USB × 1 (v4.1)
INSERT INTO device_type_ports (device_type_id, cable_type_id, label_prefix, count, edge, sort_order)
    SELECT id, 2, 'USB',   1, 'bottom', 0 FROM device_types WHERE name='Mouse' AND project_id IS NULL;
