-- mCables v4.1 setup templates. See docs/design.md §2.4.
--
-- A template is a named recipe of (device_types + requirements) that
-- bootstraps a project from blank to solver-ready in one apply call.

CREATE TABLE setup_templates (
    id              INTEGER PRIMARY KEY,
    name            TEXT NOT NULL UNIQUE,
    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'))
);

CREATE TABLE setup_template_devices (
    id              INTEGER PRIMARY KEY,
    template_id     INTEGER NOT NULL REFERENCES setup_templates(id) ON DELETE CASCADE,
    device_type_id  INTEGER NOT NULL REFERENCES device_types(id)    ON DELETE RESTRICT,
    suggested_name  TEXT,
    sort_order      INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX setup_template_devices_template_idx ON setup_template_devices(template_id);

CREATE TABLE setup_template_requirements (
    id                       INTEGER PRIMARY KEY,
    template_id              INTEGER NOT NULL REFERENCES setup_templates(id) ON DELETE CASCADE,
    from_template_device_id  INTEGER NOT NULL REFERENCES setup_template_devices(id) ON DELETE CASCADE,
    to_template_device_id    INTEGER NOT NULL REFERENCES setup_template_devices(id) ON DELETE CASCADE,
    preferred_cable_type_id  INTEGER REFERENCES cable_types(id) ON DELETE SET NULL,
    must_connect             INTEGER NOT NULL DEFAULT 1 CHECK (must_connect IN (0, 1)),
    CHECK (from_template_device_id != to_template_device_id)
);
CREATE INDEX setup_template_reqs_template_idx ON setup_template_requirements(template_id);

-- ---------------------------------------------------------------- Living Room
INSERT INTO setup_templates (name, description, built_in)
    VALUES ('Living Room', 'TV + Soundbar + ChromeCast, HDMI between them.', 1);

INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Living Room'),
        (SELECT id FROM device_types WHERE name='TV' AND project_id IS NULL),
        'TV', 0;
INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Living Room'),
        (SELECT id FROM device_types WHERE name='Soundbar' AND project_id IS NULL),
        'Soundbar', 1;
INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Living Room'),
        (SELECT id FROM device_types WHERE name='ChromeCast' AND project_id IS NULL),
        'ChromeCast', 2;

-- TV ↔ Soundbar (HDMI, must)
INSERT INTO setup_template_requirements
    (template_id, from_template_device_id, to_template_device_id, preferred_cable_type_id, must_connect)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Living Room'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Living Room') AND suggested_name='TV'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Living Room') AND suggested_name='Soundbar'),
        3, 1;
-- TV ↔ ChromeCast (HDMI, must)
INSERT INTO setup_template_requirements
    (template_id, from_template_device_id, to_template_device_id, preferred_cable_type_id, must_connect)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Living Room'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Living Room') AND suggested_name='TV'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Living Room') AND suggested_name='ChromeCast'),
        3, 1;

-- ---------------------------------------------------------------- Home Office
INSERT INTO setup_templates (name, description, built_in)
    VALUES ('Home Office', 'PC + Screen + Keyboard + Mouse. HDMI + USB.', 1);

INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Home Office'),
        (SELECT id FROM device_types WHERE name='PC' AND project_id IS NULL),
        'PC', 0;
INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Home Office'),
        (SELECT id FROM device_types WHERE name='Screen' AND project_id IS NULL),
        'Screen', 1;
INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Home Office'),
        (SELECT id FROM device_types WHERE name='Keyboard' AND project_id IS NULL),
        'Keyboard', 2;
INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Home Office'),
        (SELECT id FROM device_types WHERE name='Mouse' AND project_id IS NULL),
        'Mouse', 3;

-- PC ↔ Screen (HDMI, must)
INSERT INTO setup_template_requirements
    (template_id, from_template_device_id, to_template_device_id, preferred_cable_type_id, must_connect)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Home Office'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Home Office') AND suggested_name='PC'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Home Office') AND suggested_name='Screen'),
        3, 1;
-- PC ↔ Keyboard (USB, must)
INSERT INTO setup_template_requirements
    (template_id, from_template_device_id, to_template_device_id, preferred_cable_type_id, must_connect)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Home Office'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Home Office') AND suggested_name='PC'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Home Office') AND suggested_name='Keyboard'),
        2, 1;
-- PC ↔ Mouse (USB, must)
INSERT INTO setup_template_requirements
    (template_id, from_template_device_id, to_template_device_id, preferred_cable_type_id, must_connect)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Home Office'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Home Office') AND suggested_name='PC'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Home Office') AND suggested_name='Mouse'),
        2, 1;

-- ---------------------------------------------------------------- Server Rack
INSERT INTO setup_templates (name, description, built_in)
    VALUES ('Server Rack', 'NAS + Switch + fritz. Ethernet trunk + power.', 1);

INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Server Rack'),
        (SELECT id FROM device_types WHERE name='NAS' AND project_id IS NULL),
        'NAS', 0;
INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Server Rack'),
        (SELECT id FROM device_types WHERE name='Switch' AND project_id IS NULL),
        'Switch', 1;
INSERT INTO setup_template_devices (template_id, device_type_id, suggested_name, sort_order)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Server Rack'),
        (SELECT id FROM device_types WHERE name='fritz' AND project_id IS NULL),
        'fritz', 2;

-- NAS ↔ Switch (RJ45, must)
INSERT INTO setup_template_requirements
    (template_id, from_template_device_id, to_template_device_id, preferred_cable_type_id, must_connect)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Server Rack'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Server Rack') AND suggested_name='NAS'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Server Rack') AND suggested_name='Switch'),
        5, 1;
-- Switch ↔ fritz (RJ45, must)
INSERT INTO setup_template_requirements
    (template_id, from_template_device_id, to_template_device_id, preferred_cable_type_id, must_connect)
    SELECT
        (SELECT id FROM setup_templates WHERE name='Server Rack'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Server Rack') AND suggested_name='Switch'),
        (SELECT id FROM setup_template_devices WHERE template_id = (SELECT id FROM setup_templates WHERE name='Server Rack') AND suggested_name='fritz'),
        5, 1;
