-- ============================================================================
-- KundenKarte Module Update 3.3.0
-- Correct terminal configurations (bidirectional format)
-- ============================================================================

-- FI (Fehlerstromschutzschalter) - 4 Terminals (2 oben: L+N, 2 unten: L+N)
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"L","pos":"top"},{"id":"t2","label":"N","pos":"top"},{"id":"t3","label":"L","pos":"bottom"},{"id":"t4","label":"N","pos":"bottom"}]}'
WHERE ref IN ('FI', 'RCD', 'RCCB');

-- FI/LS Kombi - 4 Terminals
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"L","pos":"top"},{"id":"t2","label":"N","pos":"top"},{"id":"t3","label":"L","pos":"bottom"},{"id":"t4","label":"N","pos":"bottom"}]}'
WHERE ref IN ('FILS', 'RCBO');

-- HS (Hauptschalter) - 3 Pole (L1, L2, L3) ohne N
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"L1","pos":"top"},{"id":"t2","label":"L2","pos":"top"},{"id":"t3","label":"L3","pos":"top"},{"id":"t4","label":"L1","pos":"bottom"},{"id":"t5","label":"L2","pos":"bottom"},{"id":"t6","label":"L3","pos":"bottom"}]}'
WHERE ref IN ('HS', 'HAUPTSCHALTER');

-- LS (Leitungsschutzschalter) - 2 Terminals (1 oben, 1 unten)
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"●","pos":"top"},{"id":"t2","label":"●","pos":"bottom"}]}'
WHERE ref IN ('LS', 'LSS', 'B16', 'C16') AND (terminals_config IS NULL OR terminals_config LIKE '%inputs%');

-- 3-poliger LS - 6 Terminals
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"L1","pos":"top"},{"id":"t2","label":"L2","pos":"top"},{"id":"t3","label":"L3","pos":"top"},{"id":"t4","label":"L1","pos":"bottom"},{"id":"t5","label":"L2","pos":"bottom"},{"id":"t6","label":"L3","pos":"bottom"}]}'
WHERE ref IN ('LS3P', 'C3P') AND (terminals_config IS NULL OR terminals_config LIKE '%inputs%');

-- 4-poliger FI - 8 Terminals (3P+N)
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"L1","pos":"top"},{"id":"t2","label":"L2","pos":"top"},{"id":"t3","label":"L3","pos":"top"},{"id":"t4","label":"N","pos":"top"},{"id":"t5","label":"L1","pos":"bottom"},{"id":"t6","label":"L2","pos":"bottom"},{"id":"t7","label":"L3","pos":"bottom"},{"id":"t8","label":"N","pos":"bottom"}]}'
WHERE ref IN ('FI4P', 'RCD4P') AND (terminals_config IS NULL OR terminals_config LIKE '%inputs%');

-- Schuetz/Relais - 4 Terminals
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"A1","pos":"top"},{"id":"t2","label":"1","pos":"top"},{"id":"t3","label":"A2","pos":"bottom"},{"id":"t4","label":"2","pos":"bottom"}]}'
WHERE ref IN ('SCHUETZ', 'RELAIS', 'K') AND (terminals_config IS NULL OR terminals_config LIKE '%inputs%');

-- Klemme - 2 Terminals
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"●","pos":"top"},{"id":"t2","label":"●","pos":"bottom"}]}'
WHERE ref IN ('KLEMME', 'REIHENKLEMME', 'RK') AND (terminals_config IS NULL OR terminals_config LIKE '%inputs%');

-- Ueberspannungsschutz - 5 Terminals (3P+N oben, PE unten)
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"L1","pos":"top"},{"id":"t2","label":"L2","pos":"top"},{"id":"t3","label":"L3","pos":"top"},{"id":"t4","label":"N","pos":"top"},{"id":"t5","label":"PE","pos":"bottom"}]}'
WHERE ref IN ('SPD', 'UESP') AND (terminals_config IS NULL OR terminals_config LIKE '%inputs%');

-- Default: 2 Terminals fuer alle ohne Konfiguration
UPDATE llx_kundenkarte_equipment_type
SET terminals_config = '{"terminals":[{"id":"t1","label":"●","pos":"top"},{"id":"t2","label":"●","pos":"bottom"}]}'
WHERE terminals_config IS NULL OR terminals_config = '';
