-- ============================================================================
-- Copyright (C) 2026 Alles Watt lauft
--
-- Building Types (Gebäudetypen) - global structure elements
-- These are system-independent and can be used across all installations
-- Examples: Haus, Stall, Halle, Saal, Eingangshalle, Flur, Zimmer, Südflügel
-- ============================================================================

CREATE TABLE IF NOT EXISTS llx_kundenkarte_building_type
(
    rowid               integer AUTO_INCREMENT PRIMARY KEY,
    entity              integer DEFAULT 0 NOT NULL,

    ref                 varchar(50) NOT NULL,
    label               varchar(128) NOT NULL,
    label_short         varchar(32),
    description         text,

    -- Hierarchy
    fk_parent           integer DEFAULT 0,      -- Parent building type (0 = root)
    level_type          varchar(32),            -- 'building', 'floor', 'wing', 'room', 'area'

    -- Display
    icon                varchar(64),            -- FontAwesome icon class
    color               varchar(20),
    picto               varchar(128),

    -- Settings
    is_system           tinyint DEFAULT 0,      -- System-defined (not deletable)
    can_have_children   tinyint DEFAULT 1,      -- Can contain sub-elements
    position            integer DEFAULT 0,
    active              tinyint DEFAULT 1,

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

    UNIQUE KEY uk_building_type_ref (ref, entity),
    INDEX idx_building_parent (fk_parent),
    INDEX idx_building_level (level_type)
) ENGINE=innodb;
