-- ============================================================================
-- KundenKarte Module Update 3.2.0
-- Add terminals configuration for equipment types
-- ============================================================================

-- Add terminals_config field to equipment_type table
-- JSON format for defining input/output terminals
-- Example: {"inputs": [{"id": "in_L", "label": "L"}, {"id": "in_N", "label": "N"}], "outputs": [{"id": "out_L", "label": "L"}, {"id": "out_N", "label": "N"}]}
ALTER TABLE llx_kundenkarte_equipment_type
ADD COLUMN terminals_config TEXT DEFAULT NULL COMMENT 'JSON config for terminals (inputs/outputs)';

-- Add free position fields for equipment in the schematic editor
ALTER TABLE llx_kundenkarte_equipment
ADD COLUMN editor_x INTEGER DEFAULT NULL COMMENT 'X position in schematic editor',
ADD COLUMN editor_y INTEGER DEFAULT NULL COMMENT 'Y position in schematic editor';

-- Update connection table to support terminal-based connections
ALTER TABLE llx_kundenkarte_equipment_connection
ADD COLUMN source_terminal_id VARCHAR(64) DEFAULT NULL COMMENT 'Source terminal ID (e.g., out_L, out_N)',
ADD COLUMN target_terminal_id VARCHAR(64) DEFAULT NULL COMMENT 'Target terminal ID (e.g., in_L, in_N)';

-- Default terminal configurations for common equipment types
-- These can be customized by the user

-- LS (Leitungsschutzschalter) - 1 input, 1 output
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in","label":"●","pos":"top"}],"outputs":[{"id":"out","label":"●","pos":"bottom"}]}'
WHERE ref IN ('LS', 'LSS', 'B16', 'C16') AND terminals_config IS NULL;

-- FI (Fehlerstromschutzschalter) - 2 inputs, 2 outputs (L+N)
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in_L","label":"L","pos":"top-left"},{"id":"in_N","label":"N","pos":"top-right"}],"outputs":[{"id":"out_L","label":"L","pos":"bottom-left"},{"id":"out_N","label":"N","pos":"bottom-right"}]}'
WHERE ref IN ('FI', 'RCD', 'RCCB') AND terminals_config IS NULL;

-- FI/LS Kombi - 2 inputs, 2 outputs
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in_L","label":"L","pos":"top-left"},{"id":"in_N","label":"N","pos":"top-right"}],"outputs":[{"id":"out_L","label":"L","pos":"bottom-left"},{"id":"out_N","label":"N","pos":"bottom-right"}]}'
WHERE ref IN ('FILS', 'RCBO') AND terminals_config IS NULL;

-- 3-poliger LS - 3 inputs, 3 outputs
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in_L1","label":"L1","pos":"top"},{"id":"in_L2","label":"L2","pos":"top"},{"id":"in_L3","label":"L3","pos":"top"}],"outputs":[{"id":"out_L1","label":"L1","pos":"bottom"},{"id":"out_L2","label":"L2","pos":"bottom"},{"id":"out_L3","label":"L3","pos":"bottom"}]}'
WHERE ref IN ('LS3P', 'C3P') AND terminals_config IS NULL;

-- 4-poliger FI - 4 inputs, 4 outputs
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in_L1","label":"L1","pos":"top"},{"id":"in_L2","label":"L2","pos":"top"},{"id":"in_L3","label":"L3","pos":"top"},{"id":"in_N","label":"N","pos":"top"}],"outputs":[{"id":"out_L1","label":"L1","pos":"bottom"},{"id":"out_L2","label":"L2","pos":"bottom"},{"id":"out_L3","label":"L3","pos":"bottom"},{"id":"out_N","label":"N","pos":"bottom"}]}'
WHERE ref IN ('FI4P', 'RCD4P') AND terminals_config IS NULL;

-- Hauptschalter - variable (default 3P+N)
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in_L1","label":"L1","pos":"top"},{"id":"in_L2","label":"L2","pos":"top"},{"id":"in_L3","label":"L3","pos":"top"},{"id":"in_N","label":"N","pos":"top"}],"outputs":[{"id":"out_L1","label":"L1","pos":"bottom"},{"id":"out_L2","label":"L2","pos":"bottom"},{"id":"out_L3","label":"L3","pos":"bottom"},{"id":"out_N","label":"N","pos":"bottom"}]}'
WHERE ref IN ('HS', 'HAUPTSCHALTER') AND terminals_config IS NULL;

-- Schütz/Relais - 2 inputs, 2 outputs (Steuerstromkreis + Lastkreis)
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in_A1","label":"A1","pos":"top-left"},{"id":"in_1","label":"1","pos":"top-right"}],"outputs":[{"id":"out_A2","label":"A2","pos":"bottom-left"},{"id":"out_2","label":"2","pos":"bottom-right"}]}'
WHERE ref IN ('SCHUETZ', 'RELAIS', 'K') AND terminals_config IS NULL;

-- Stromstoßschalter - Steuerung + 2 Lastanschlüsse
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in_A1","label":"A1","pos":"top-left"},{"id":"in_1","label":"1","pos":"top-right"}],"outputs":[{"id":"out_A2","label":"A2","pos":"bottom-left"},{"id":"out_2","label":"2","pos":"bottom-right"}]}'
WHERE ref IN ('SSS', 'STROMSTOSS') AND terminals_config IS NULL;

-- Klemme - durchgehend
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in","label":"●","pos":"top"}],"outputs":[{"id":"out","label":"●","pos":"bottom"}]}'
WHERE ref IN ('KLEMME', 'REIHENKLEMME', 'RK') AND terminals_config IS NULL;

-- Überspannungsschutz - 3P+N+PE
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in_L1","label":"L1","pos":"top"},{"id":"in_L2","label":"L2","pos":"top"},{"id":"in_L3","label":"L3","pos":"top"},{"id":"in_N","label":"N","pos":"top"}],"outputs":[{"id":"out_PE","label":"PE","pos":"bottom"}]}'
WHERE ref IN ('SPD', 'UESP') AND terminals_config IS NULL;

-- Default for any equipment without config: 1 input, 1 output
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"inputs":[{"id":"in","label":"●","pos":"top"}],"outputs":[{"id":"out","label":"●","pos":"bottom"}]}'
WHERE terminals_config IS NULL;
