-- Copyright (C) 2026 Alles Watt lauft
--
-- Terminal bridges (Brücken zwischen Reihenklemmen)
-- Connects terminals of adjacent terminal blocks horizontally

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

    fk_anlage       INTEGER NOT NULL,           -- Installation reference
    fk_carrier      INTEGER NOT NULL,           -- Carrier where bridge is placed

    -- Bridge spans from start_te to end_te, connecting terminals horizontally
    start_te        INTEGER NOT NULL,           -- Start position (TE)
    end_te          INTEGER NOT NULL,           -- End position (TE)

    -- Terminal position
    terminal_side   VARCHAR(10) DEFAULT 'top',  -- 'top' or 'bottom' side of blocks
    terminal_row    INTEGER DEFAULT 0,          -- Row index for stacked terminals

    -- Visual properties
    color           VARCHAR(20) DEFAULT '#e74c3c',  -- Bridge color (red default)
    bridge_type     VARCHAR(20) DEFAULT 'standard', -- 'standard', 'phase', 'pe', 'n'
    label           VARCHAR(50),                -- Optional label

    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_terminal_bridge ADD INDEX idx_bridge_anlage (fk_anlage);
ALTER TABLE llx_kundenkarte_terminal_bridge ADD INDEX idx_bridge_carrier (fk_carrier);

-- Foreign keys
ALTER TABLE llx_kundenkarte_terminal_bridge ADD CONSTRAINT fk_bridge_anlage
    FOREIGN KEY (fk_anlage) REFERENCES llx_kundenkarte_anlage(rowid) ON DELETE CASCADE;
ALTER TABLE llx_kundenkarte_terminal_bridge ADD CONSTRAINT fk_bridge_carrier
    FOREIGN KEY (fk_carrier) REFERENCES llx_kundenkarte_equipment_carrier(rowid) ON DELETE CASCADE;
