-- Copyright (C) 2026 Alles Watt lauft
--
-- Equipment connections (Verbindungen zwischen Equipment)
-- Generic connection system for any installation type (electrical, network, security, etc.)

CREATE TABLE llx_kundenkarte_equipment_connection (
    rowid           INTEGER AUTO_INCREMENT PRIMARY KEY,
    entity          INTEGER DEFAULT 1 NOT NULL,

    -- Source equipment (where connection comes FROM)
    fk_source       INTEGER,                    -- NULL = external input
    source_terminal VARCHAR(20) DEFAULT 'output', -- Terminal identifier (flexible)

    -- Target equipment (where connection goes TO)
    fk_target       INTEGER,                    -- NULL = output/endpoint
    target_terminal VARCHAR(20) DEFAULT 'input',  -- Terminal identifier (flexible)

    -- Connection properties (flexible, user-defined)
    connection_type VARCHAR(50),                -- User-defined type (e.g., "L1N", "CAT6", "2-Draht", etc.)
    color           VARCHAR(20),                -- Display color (hex code)

    -- Output/endpoint info (when fk_target is NULL)
    output_label    VARCHAR(255),               -- e.g., "Küche Steckdosen", "Büro PC1", "Haustür"

    -- Medium info (cable, wire, etc.) - all flexible text fields
    medium_type     VARCHAR(100),               -- e.g., "NYM-J", "CAT6 S/FTP", "2x0.8 J-Y(St)Y"
    medium_spec     VARCHAR(100),               -- e.g., "3x2.5", "AWG23", "Ring 1"
    medium_length   VARCHAR(50),                -- Length as text (allows "ca. 15m", "5-10m", etc.)

    -- Rail/bar connections (spans multiple equipment)
    is_rail         TINYINT DEFAULT 0,          -- 1 = this is a rail/bar spanning multiple slots
    rail_start_te   INTEGER,                    -- Start position on carrier
    rail_end_te     INTEGER,                    -- End position on carrier
    rail_phases     VARCHAR(20),                -- '3P', '3P+N', 'L1', 'L1N', etc. for multi-line display
    excluded_te     VARCHAR(100),               -- Comma-separated TE positions to exclude (gaps for FI)

    fk_carrier      INTEGER,                    -- Carrier where this connection is rendered
    position_y      INTEGER DEFAULT 0,          -- Y offset for rendering (0=first row, 1=second row, etc.)

    -- Manual path data (SVG path string for manually drawn connections)
    path_data       TEXT,                       -- SVG path like "M 100 200 L 150 200 L 150 300"

    note_private    TEXT,
    status          INTEGER DEFAULT 1,

    date_creation   DATETIME,
    tms             TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    fk_user_creat   INTEGER,
    fk_user_modif   INTEGER
) ENGINE=InnoDB;

-- Indexes
ALTER TABLE llx_kundenkarte_equipment_connection ADD INDEX idx_connection_source (fk_source);
ALTER TABLE llx_kundenkarte_equipment_connection ADD INDEX idx_connection_target (fk_target);
ALTER TABLE llx_kundenkarte_equipment_connection ADD INDEX idx_connection_carrier (fk_carrier);
ALTER TABLE llx_kundenkarte_equipment_connection ADD INDEX idx_connection_entity (entity);

-- Foreign keys
ALTER TABLE llx_kundenkarte_equipment_connection ADD CONSTRAINT fk_connection_source
    FOREIGN KEY (fk_source) REFERENCES llx_kundenkarte_equipment(rowid) ON DELETE CASCADE;
ALTER TABLE llx_kundenkarte_equipment_connection ADD CONSTRAINT fk_connection_target
    FOREIGN KEY (fk_target) REFERENCES llx_kundenkarte_equipment(rowid) ON DELETE CASCADE;
ALTER TABLE llx_kundenkarte_equipment_connection ADD CONSTRAINT fk_connection_carrier
    FOREIGN KEY (fk_carrier) REFERENCES llx_kundenkarte_equipment_carrier(rowid) ON DELETE CASCADE;
