-- ============================================================================
-- Copyright (C) 2026 Alles Watt lauft
--
-- Anlage Connections (Verbindungen zwischen Anlagen-Elementen im Baum)
-- Beschreibt Kabel/Leitungen zwischen Strukturelementen wie HAK → Zählerschrank
-- ============================================================================

CREATE TABLE IF NOT EXISTS llx_kundenkarte_anlage_connection
(
    rowid               integer AUTO_INCREMENT PRIMARY KEY,
    entity              integer DEFAULT 1 NOT NULL,

    -- Source and target anlagen
    fk_source           integer NOT NULL COMMENT 'Source Anlage ID',
    fk_target           integer NOT NULL COMMENT 'Target Anlage ID',

    -- Connection description
    label               varchar(255) COMMENT 'Connection label/description',

    -- Medium/Cable info (references medium_type table or free text)
    fk_medium_type      integer COMMENT 'Reference to medium_type table',
    medium_type_text    varchar(100) COMMENT 'Free text if no type selected',
    medium_spec         varchar(100) COMMENT 'Specification (e.g., 5x16)',
    medium_length       varchar(50) COMMENT 'Length (e.g., 15m)',
    medium_color        varchar(50) COMMENT 'Wire/cable color',

    -- Additional info
    route_description   text COMMENT 'Description of cable route',
    installation_date   date COMMENT 'When was this installed',

    -- Status
    status              integer DEFAULT 1,
    note_private        text,
    note_public         text,

    date_creation       datetime,
    tms                 timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    fk_user_creat       integer,
    fk_user_modif       integer,

    INDEX idx_anlage_conn_source (fk_source),
    INDEX idx_anlage_conn_target (fk_target),
    CONSTRAINT fk_anlage_conn_source FOREIGN KEY (fk_source) REFERENCES llx_kundenkarte_anlage(rowid) ON DELETE CASCADE,
    CONSTRAINT fk_anlage_conn_target FOREIGN KEY (fk_target) REFERENCES llx_kundenkarte_anlage(rowid) ON DELETE CASCADE
) ENGINE=innodb;
