-- ============================================================================
-- KundenKarte Module Update 3.3.2
-- Add GLOBAL building element types (available in ALL systems)
-- ============================================================================
-- Gebäude-Typen werden in einem speziellen "GLOBAL" System angelegt.
-- Das System hat den Code "GLOBAL" und wird in der fetchAllBySystem()
-- Methode so behandelt, dass seine Typen in allen Systemen erscheinen.
-- ============================================================================

-- ============================================================================
-- 1. First, clean up any previously created "Gebäude" system and types
-- ============================================================================

-- Delete fields for Gebäude types (if any exist from previous attempt)
DELETE f FROM llx_kundenkarte_anlage_type_field f
INNER JOIN llx_kundenkarte_anlage_type t ON f.fk_anlage_type = t.rowid
INNER JOIN llx_c_kundenkarte_anlage_system s ON t.fk_system = s.rowid
WHERE s.code = 'GEBAEUDE';

-- Delete Gebäude types (if any exist from previous attempt)
DELETE t FROM llx_kundenkarte_anlage_type t
INNER JOIN llx_c_kundenkarte_anlage_system s ON t.fk_system = s.rowid
WHERE s.code = 'GEBAEUDE';

-- Delete Gebäude system (not needed anymore - we use GLOBAL instead)
DELETE FROM llx_c_kundenkarte_anlage_system WHERE code = 'GEBAEUDE';

-- ============================================================================
-- 2. Create the GLOBAL system (for types available in ALL systems)
-- ============================================================================

INSERT IGNORE INTO llx_c_kundenkarte_anlage_system (entity, code, label, picto, color, position, active)
VALUES (0, 'GLOBAL', 'Gebäude & Standort', 'fa-building', '#3498db', 0, 1);

-- Get the GLOBAL system ID for further inserts
SET @global_system_id = (SELECT rowid FROM llx_c_kundenkarte_anlage_system WHERE code = 'GLOBAL' LIMIT 1);

-- ============================================================================
-- 3. Add GLOBAL Building Element Types
-- ============================================================================

-- BUILDING LEVEL (Gebaeude) - Top level structures
INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'HAUS', 'Haus', 'Haus', @global_system_id, 1, 0, 'fa-home', '#3498db', 1, 10, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'HALLE', 'Halle', 'Halle', @global_system_id, 1, 0, 'fa-warehouse', '#e67e22', 1, 20, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'STALL', 'Stall', 'Stall', @global_system_id, 1, 0, 'fa-horse', '#8e44ad', 1, 30, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'GARAGE', 'Garage', 'Garage', @global_system_id, 1, 0, 'fa-car', '#2c3e50', 1, 40, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'SCHUPPEN', 'Schuppen', 'Schuppen', @global_system_id, 1, 0, 'fa-box', '#7f8c8d', 1, 50, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'BUEROGEBAEUDE', 'Bürogebäude', 'Büro', @global_system_id, 1, 0, 'fa-building', '#1abc9c', 1, 60, 1, NOW());

-- FLOOR LEVEL (Etage/Geschoss)
INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'KELLER', 'Keller', 'KG', @global_system_id, 1, 0, 'HAUS,HALLE,STALL,BUEROGEBAEUDE', 'fa-level-down-alt', '#34495e', 1, 100, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'ERDGESCHOSS', 'Erdgeschoss', 'EG', @global_system_id, 1, 0, 'HAUS,HALLE,STALL,BUEROGEBAEUDE,GARAGE', 'fa-layer-group', '#27ae60', 1, 110, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'OBERGESCHOSS', 'Obergeschoss', 'OG', @global_system_id, 1, 0, 'HAUS,HALLE,BUEROGEBAEUDE', 'fa-level-up-alt', '#2980b9', 1, 120, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'DACHGESCHOSS', 'Dachgeschoss', 'DG', @global_system_id, 1, 0, 'HAUS,BUEROGEBAEUDE', 'fa-home', '#9b59b6', 1, 130, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'SPITZBODEN', 'Spitzboden', 'SB', @global_system_id, 1, 0, 'HAUS,DACHGESCHOSS', 'fa-mountain', '#95a5a6', 1, 140, 1, NOW());

-- WING LEVEL (Gebaeudeteile/Trakte)
INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'NORDFLUEGL', 'Nordflügel', 'Nord', @global_system_id, 1, 0, 'HAUS,HALLE,BUEROGEBAEUDE,KELLER,ERDGESCHOSS,OBERGESCHOSS,DACHGESCHOSS', 'fa-compass', '#3498db', 1, 200, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'SUEDFLUEGL', 'Südflügel', 'Süd', @global_system_id, 1, 0, 'HAUS,HALLE,BUEROGEBAEUDE,KELLER,ERDGESCHOSS,OBERGESCHOSS,DACHGESCHOSS', 'fa-compass', '#e74c3c', 1, 210, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'OSTFLUEGL', 'Ostflügel', 'Ost', @global_system_id, 1, 0, 'HAUS,HALLE,BUEROGEBAEUDE,KELLER,ERDGESCHOSS,OBERGESCHOSS,DACHGESCHOSS', 'fa-compass', '#f39c12', 1, 220, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'WESTFLUEGL', 'Westflügel', 'West', @global_system_id, 1, 0, 'HAUS,HALLE,BUEROGEBAEUDE,KELLER,ERDGESCHOSS,OBERGESCHOSS,DACHGESCHOSS', 'fa-compass', '#2ecc71', 1, 230, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, allowed_parent_types, picto, color, is_system, position, active, date_creation)
VALUES (0, 'ANBAU', 'Anbau', 'Anbau', @global_system_id, 1, 0, 'HAUS,HALLE,BUEROGEBAEUDE', 'fa-plus-square', '#1abc9c', 1, 240, 1, NOW());

-- CORRIDOR LEVEL (Flure/Gaenge)
INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'FLUR', 'Flur', 'Flur', @global_system_id, 1, 0, 'fa-arrows-alt-h', '#16a085', 1, 300, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'EINGANGSHALLE', 'Eingangshalle', 'Eingang', @global_system_id, 1, 0, 'fa-door-open', '#2c3e50', 1, 310, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'TREPPENHAUS', 'Treppenhaus', 'Treppe', @global_system_id, 1, 0, 'fa-stairs', '#8e44ad', 1, 320, 1, NOW());

-- ROOM LEVEL (Raeume)
INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'ZIMMER', 'Zimmer', 'Zi', @global_system_id, 1, 0, 'fa-door-closed', '#3498db', 1, 400, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'WOHNZIMMER', 'Wohnzimmer', 'WoZi', @global_system_id, 1, 0, 'fa-couch', '#e67e22', 1, 410, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'SCHLAFZIMMER', 'Schlafzimmer', 'SchZi', @global_system_id, 1, 0, 'fa-bed', '#9b59b6', 1, 420, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'KINDERZIMMER', 'Kinderzimmer', 'KiZi', @global_system_id, 1, 0, 'fa-child', '#1abc9c', 1, 430, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'KUECHE', 'Küche', 'Kü', @global_system_id, 1, 0, 'fa-utensils', '#27ae60', 1, 440, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'BAD', 'Badezimmer', 'Bad', @global_system_id, 1, 0, 'fa-bath', '#3498db', 1, 450, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'WC', 'WC/Toilette', 'WC', @global_system_id, 1, 0, 'fa-toilet', '#95a5a6', 1, 460, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'BUERO', 'Büro', 'Büro', @global_system_id, 1, 0, 'fa-desktop', '#2c3e50', 1, 470, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'ABSTELLRAUM', 'Abstellraum', 'Abst', @global_system_id, 1, 0, 'fa-box', '#7f8c8d', 1, 480, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'HAUSWIRTSCHAFT', 'Hauswirtschaftsraum', 'HWR', @global_system_id, 1, 0, 'fa-tshirt', '#16a085', 1, 490, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'WERKSTATT', 'Werkstatt', 'Werkst', @global_system_id, 1, 0, 'fa-tools', '#f39c12', 1, 500, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'TECHNIKRAUM', 'Technikraum', 'Tech', @global_system_id, 1, 0, 'fa-cogs', '#e74c3c', 1, 510, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'SERVERRAUM', 'Serverraum', 'Server', @global_system_id, 1, 0, 'fa-server', '#8e44ad', 1, 520, 1, NOW());

-- AREA LEVEL (Aussenbereiche)
INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'AUSSENBEREICH', 'Außenbereich', 'Außen', @global_system_id, 1, 0, 'fa-tree', '#27ae60', 1, 600, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'TERRASSE', 'Terrasse', 'Terrasse', @global_system_id, 1, 0, 'fa-sun', '#f1c40f', 1, 610, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'GARTEN', 'Garten', 'Garten', @global_system_id, 1, 0, 'fa-leaf', '#2ecc71', 1, 620, 1, NOW());

INSERT IGNORE INTO llx_kundenkarte_anlage_type (entity, ref, label, label_short, fk_system, can_have_children, can_be_nested, picto, color, is_system, position, active, date_creation)
VALUES (0, 'CARPORT', 'Carport', 'Carport', @global_system_id, 1, 0, 'fa-car-side', '#34495e', 1, 630, 1, NOW());

-- ============================================================================
-- 4. Add default fields for building types
-- ============================================================================

-- Raumnummer field for rooms
INSERT IGNORE INTO llx_kundenkarte_anlage_type_field (entity, fk_anlage_type, field_code, field_label, field_type, required, field_options, position, active, date_creation)
SELECT 0, t.rowid, 'raumnummer', 'Raumnummer', 'varchar', 0, NULL, 10, 1, NOW()
FROM llx_kundenkarte_anlage_type t
INNER JOIN llx_c_kundenkarte_anlage_system s ON t.fk_system = s.rowid
WHERE s.code = 'GLOBAL'
AND t.ref IN ('ZIMMER', 'WOHNZIMMER', 'SCHLAFZIMMER', 'KINDERZIMMER', 'KUECHE', 'BAD', 'WC', 'BUERO', 'ABSTELLRAUM', 'HAUSWIRTSCHAFT', 'WERKSTATT', 'TECHNIKRAUM', 'SERVERRAUM');

-- Flaeche field for all building types
INSERT IGNORE INTO llx_kundenkarte_anlage_type_field (entity, fk_anlage_type, field_code, field_label, field_type, required, field_options, position, active, date_creation)
SELECT 0, t.rowid, 'flaeche', 'Fläche (m²)', 'varchar', 0, NULL, 20, 1, NOW()
FROM llx_kundenkarte_anlage_type t
INNER JOIN llx_c_kundenkarte_anlage_system s ON t.fk_system = s.rowid
WHERE s.code = 'GLOBAL'
AND t.ref IN ('HAUS', 'HALLE', 'STALL', 'GARAGE', 'SCHUPPEN', 'BUEROGEBAEUDE', 'KELLER', 'ERDGESCHOSS', 'OBERGESCHOSS', 'DACHGESCHOSS', 'SPITZBODEN', 'ZIMMER', 'WOHNZIMMER', 'SCHLAFZIMMER', 'KINDERZIMMER', 'KUECHE', 'BAD', 'WC', 'BUERO', 'ABSTELLRAUM', 'HAUSWIRTSCHAFT', 'WERKSTATT', 'TECHNIKRAUM', 'SERVERRAUM', 'AUSSENBEREICH', 'TERRASSE', 'GARTEN');

-- Baujahr field for main buildings
INSERT IGNORE INTO llx_kundenkarte_anlage_type_field (entity, fk_anlage_type, field_code, field_label, field_type, required, field_options, position, active, date_creation)
SELECT 0, t.rowid, 'baujahr', 'Baujahr', 'varchar', 0, NULL, 30, 1, NOW()
FROM llx_kundenkarte_anlage_type t
INNER JOIN llx_c_kundenkarte_anlage_system s ON t.fk_system = s.rowid
WHERE s.code = 'GLOBAL'
AND t.ref IN ('HAUS', 'HALLE', 'STALL', 'GARAGE', 'SCHUPPEN', 'BUEROGEBAEUDE', 'ANBAU');
