--
-- Script run when an upgrade of Dolibarr is done. Whatever is the Dolibarr version.
--

-- Add path_data column for manually drawn connection paths
ALTER TABLE llx_kundenkarte_equipment_connection ADD COLUMN IF NOT EXISTS path_data TEXT AFTER position_y;

-- Add icon_file column for custom SVG/PNG schematic symbols
ALTER TABLE llx_kundenkarte_equipment_type ADD COLUMN IF NOT EXISTS icon_file VARCHAR(255) AFTER picto;

-- Add terminals_config if not exists
ALTER TABLE llx_kundenkarte_equipment_type ADD COLUMN IF NOT EXISTS terminals_config TEXT AFTER fk_product;

-- Add flow_direction and terminal_position for equipment types
ALTER TABLE llx_kundenkarte_equipment_type ADD COLUMN IF NOT EXISTS flow_direction VARCHAR(16) DEFAULT NULL AFTER terminals_config;
ALTER TABLE llx_kundenkarte_equipment_type ADD COLUMN IF NOT EXISTS terminal_position VARCHAR(16) DEFAULT 'both' AFTER flow_direction;

-- Add block_image for SchematicEditor display
ALTER TABLE llx_kundenkarte_equipment_type ADD COLUMN IF NOT EXISTS block_image VARCHAR(255) AFTER icon_file;

-- Add busbar type reference to connections
ALTER TABLE llx_kundenkarte_equipment_connection ADD COLUMN IF NOT EXISTS fk_busbar_type INTEGER DEFAULT NULL AFTER is_rail;

-- Create busbar_type table if not exists
CREATE TABLE IF NOT EXISTS llx_kundenkarte_busbar_type
(
    rowid               integer AUTO_INCREMENT PRIMARY KEY,
    entity              integer DEFAULT 0 NOT NULL,
    ref                 varchar(64) NOT NULL,
    label               varchar(255) NOT NULL,
    label_short         varchar(32),
    description         text,
    fk_system           integer NOT NULL,
    phases              varchar(20) NOT NULL,
    num_lines           integer DEFAULT 1 NOT NULL,
    color               varchar(64),
    default_color       varchar(8),
    line_height         integer DEFAULT 3,
    line_spacing        integer DEFAULT 4,
    position_default    varchar(16) DEFAULT 'below',
    fk_product          integer DEFAULT NULL,
    picto               varchar(64),
    icon_file           varchar(255),
    is_system           tinyint DEFAULT 0 NOT NULL,
    position            integer DEFAULT 0,
    active              tinyint DEFAULT 1 NOT NULL,
    date_creation       datetime,
    tms                 timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    fk_user_creat       integer,
    fk_user_modif       integer,
    import_key          varchar(14)
) ENGINE=innodb;
