-- topology_api/migrations/004_layout.sql
-- Diagram layout tables for topology visualization

CREATE TABLE topology.diagram_layouts (
    id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    base_id uuid NOT NULL REFERENCES topology.bases(id),
    layout_key text NOT NULL,
    name text NOT NULL,
    canvas_width numeric NOT NULL DEFAULT 1500,
    canvas_height numeric NOT NULL DEFAULT 1900,
    is_default boolean NOT NULL DEFAULT false,
    created_at timestamptz NOT NULL DEFAULT now(),
    CONSTRAINT uq_layout_base_key UNIQUE (base_id, layout_key)
);

CREATE TABLE topology.diagram_nodes (
    id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    layout_id uuid NOT NULL REFERENCES topology.diagram_layouts(id),
    component_id uuid REFERENCES topology.components(id),
    node_key text NOT NULL,
    x numeric NOT NULL,
    y numeric NOT NULL,
    width numeric NOT NULL DEFAULT 200,
    height numeric NOT NULL DEFAULT 120,
    rotation_deg numeric NOT NULL DEFAULT 0,
    style_key text NOT NULL DEFAULT 'node-card',
    label_override text,
    z_index integer NOT NULL DEFAULT 0,
    created_at timestamptz NOT NULL DEFAULT now(),
    CONSTRAINT uq_diagram_node UNIQUE (layout_id, node_key)
);

CREATE INDEX IF NOT EXISTS idx_diagram_nodes_layout
    ON topology.diagram_nodes(layout_id);
