-- Falukant-Visual-Settings:
-- - neue Settings-Gruppe: type.settings.name = 'falukant'
-- - neue Param-Typen: falukantPortraitStyle, falukantFigureMode
-- - neue Select-Werte:
--     falukantPortraitStyle -> classic, medieval
--     falukantFigureMode    -> portraits, 3d, both
--
-- Hinweis:
-- Für diese Änderung sind nach heutigem Stand keine ALTER TABLEs nötig.
-- Es werden nur fehlende Typ-/Wertetabellen-Einträge angelegt.
--
-- Absichtlich ohne UPDATEs:
-- Auf manchen Servern existieren zusätzliche DB-Trigger/Regeln auf Tabellen,
-- die bei UPDATE implizit ein fehlendes `updated_at` erwarten. Darum hier nur
-- INSERT ... WHERE NOT EXISTS, damit das Script maximal robust bleibt.

BEGIN;

-- Settings-Gruppe anlegen
INSERT INTO type.settings (name)
SELECT 'falukant'
WHERE NOT EXISTS (
  SELECT 1
  FROM type.settings
  WHERE name = 'falukant'
);

-- Param-Typ: Portrait-Stil
INSERT INTO type.user_param (
  description,
  datatype,
  settings_id,
  order_id,
  immutable,
  min_age,
  gender,
  unit
)
SELECT
  'falukantPortraitStyle',
  'singleselect',
  s.id,
  920,
  false,
  NULL,
  NULL,
  NULL
FROM type.settings s
WHERE s.name = 'falukant'
  AND NOT EXISTS (
    SELECT 1
    FROM type.user_param p
    WHERE p.description = 'falukantPortraitStyle'
  );

-- Param-Typ: Anzeige-Modus
INSERT INTO type.user_param (
  description,
  datatype,
  settings_id,
  order_id,
  immutable,
  min_age,
  gender,
  unit
)
SELECT
  'falukantFigureMode',
  'singleselect',
  s.id,
  921,
  false,
  NULL,
  NULL,
  NULL
FROM type.settings s
WHERE s.name = 'falukant'
  AND NOT EXISTS (
    SELECT 1
    FROM type.user_param p
    WHERE p.description = 'falukantFigureMode'
  );

-- Select-Werte: falukantPortraitStyle
INSERT INTO type.user_param_value (user_param_type_id, value, order_id)
SELECT p.id, 'classic', 1
FROM type.user_param p
WHERE p.description = 'falukantPortraitStyle'
  AND NOT EXISTS (
    SELECT 1
    FROM type.user_param_value v
    WHERE v.user_param_type_id = p.id
      AND v.value = 'classic'
  );

INSERT INTO type.user_param_value (user_param_type_id, value, order_id)
SELECT p.id, 'medieval', 2
FROM type.user_param p
WHERE p.description = 'falukantPortraitStyle'
  AND NOT EXISTS (
    SELECT 1
    FROM type.user_param_value v
    WHERE v.user_param_type_id = p.id
      AND v.value = 'medieval'
  );

-- Select-Werte: falukantFigureMode
INSERT INTO type.user_param_value (user_param_type_id, value, order_id)
SELECT p.id, 'portraits', 1
FROM type.user_param p
WHERE p.description = 'falukantFigureMode'
  AND NOT EXISTS (
    SELECT 1
    FROM type.user_param_value v
    WHERE v.user_param_type_id = p.id
      AND v.value = 'portraits'
  );

INSERT INTO type.user_param_value (user_param_type_id, value, order_id)
SELECT p.id, '3d', 2
FROM type.user_param p
WHERE p.description = 'falukantFigureMode'
  AND NOT EXISTS (
    SELECT 1
    FROM type.user_param_value v
    WHERE v.user_param_type_id = p.id
      AND v.value = '3d'
  );

INSERT INTO type.user_param_value (user_param_type_id, value, order_id)
SELECT p.id, 'both', 3
FROM type.user_param p
WHERE p.description = 'falukantFigureMode'
  AND NOT EXISTS (
    SELECT 1
    FROM type.user_param_value v
    WHERE v.user_param_type_id = p.id
      AND v.value = 'both'
  );

COMMIT;

-- Prüfung 1: Gruppe + Param-Typen
SELECT
  s.name AS settings_group,
  p.id AS param_type_id,
  p.description,
  p.datatype,
  p.order_id
FROM type.user_param p
JOIN type.settings s
  ON s.id = p.settings_id
WHERE s.name = 'falukant'
ORDER BY p.order_id, p.id;

-- Prüfung 2: Select-Werte
SELECT
  p.description,
  v.id AS value_id,
  v.value,
  v.order_id
FROM type.user_param_value v
JOIN type.user_param p
  ON p.id = v.user_param_type_id
WHERE p.description IN ('falukantPortraitStyle', 'falukantFigureMode')
ORDER BY p.description, v.order_id, v.id;
