-- Sprachassistent / LLM: Einstellungen über type.settings + type.user_param + community.user_param
-- (keine Spalten mehr an community.user).
--
-- Falls du vorher add_user_llm_columns.sql ausgeführt hast: Spalten an user wieder entfernen.
ALTER TABLE community."user" DROP COLUMN IF EXISTS llm_api_key_encrypted;
ALTER TABLE community."user" DROP COLUMN IF EXISTS llm_settings;

-- Gruppe „languageAssistant“
INSERT INTO type.settings (name)
SELECT 'languageAssistant'
WHERE NOT EXISTS (SELECT 1 FROM type.settings WHERE name = 'languageAssistant');

-- Param-Typen (description eindeutig)
INSERT INTO type.user_param (description, datatype, settings_id, order_id, immutable, min_age, gender, unit)
SELECT 'llm_settings', 'string', s.id, 900, false, NULL, NULL, NULL
FROM type.settings s
WHERE s.name = 'languageAssistant'
  AND NOT EXISTS (SELECT 1 FROM type.user_param p WHERE p.description = 'llm_settings');

INSERT INTO type.user_param (description, datatype, settings_id, order_id, immutable, min_age, gender, unit)
SELECT 'llm_api_key', 'string', s.id, 901, false, NULL, NULL, NULL
FROM type.settings s
WHERE s.name = 'languageAssistant'
  AND NOT EXISTS (SELECT 1 FROM type.user_param p WHERE p.description = 'llm_api_key');
