-- CableGUI v5 — cable routing via clamps. See docs/design.md §11.
--
-- A clamp is a physical anchor placed on the canvas. A cable's polyline
-- runs from its `from` endpoint → its clamps in `ord` sequence → its
-- `to` endpoint. Cables that share an ordered pair of consecutive
-- clamps are visibly bundled along that segment (computed live by the
-- frontend; no detection pass).

CREATE TABLE clamps (
    id              INTEGER PRIMARY KEY,
    project_id      INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    x               REAL NOT NULL,
    y               REAL NOT NULL,
    label           TEXT NOT NULL DEFAULT '',
    frame_id        INTEGER REFERENCES frames(id) ON DELETE SET NULL,
    excalidraw_id   TEXT,
    created_at      TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at      TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (project_id, excalidraw_id)
);
CREATE INDEX clamps_project_idx ON clamps(project_id);
CREATE INDEX clamps_frame_idx   ON clamps(frame_id);

CREATE TABLE cable_clamps (
    cable_id        INTEGER NOT NULL REFERENCES cables(id) ON DELETE CASCADE,
    clamp_id        INTEGER NOT NULL REFERENCES clamps(id) ON DELETE CASCADE,
    ord             INTEGER NOT NULL,            -- 1-based along from→to
    PRIMARY KEY (cable_id, ord),
    UNIQUE (cable_id, clamp_id)
);
CREATE INDEX cable_clamps_clamp_idx ON cable_clamps(clamp_id);
