SELECT
    'dynamic' AS component,
    sqlpage.read_file_as_text('../shared/shell.json') AS properties;

SELECT
    'form' AS component,
    TRUE AS auto_submit;

SELECT
    'source_id' AS name,
    'Source - group' AS label,
    'select' AS "type",
    FALSE AS autocomplete,
    (
        SELECT
            jsonb_agg(
                jsonb_build_object(
                    'label',
                    format('%s - %s', src.name, lg.name),
                    'value',
                    lg.id,
                    'selected',
                    lg.id = $source_id :: int
                )
                ORDER BY
                    src.name,
                    lg.name
            )
        FROM
            public.logic_groups AS lg
            INNER JOIN public.sources AS src ON lg.source_id = src.id
    ) AS options;

SELECT
    'form' AS component,
    'Create' AS validate,
    'create.sql' AS "action";

SELECT
    'Date' AS label,
    'date' AS name,
    'date' AS "type",
    CURRENT_DATE AS value,
    6 AS width;

SELECT
    'Time' AS label,
    'time' AS name,
    'time' AS "type",
    LOCALTIME(0) AS value,
    6 AS width;

SELECT
    'UID' AS label,
    'uid' AS name,
    'SOURCE:LOGIC-GROUP:DESAMBIGUATOR:ATOM' AS placeholder,
    TRUE AS disabled;

-- ── Desambiguator Definitions Start ─────────────────────────────────
SET
    input =
SELECT
    jsonb_build_object(
        'id',
        dtp.id,
        'input_type',
        dtp.input_type,
        'placeholder',
        dtp.placeholder
    ) AS obj
FROM
    public.desambiguator_types dtp
    INNER JOIN public.sources AS src ON src.desambiguator_type_id = dtp.id
    INNER JOIN public.logic_groups AS lg ON lg.source_id = src.id
WHERE
    lg.id = $source_id :: int;

SELECT
    'logic_group_id' AS name,
    'hidden' AS TYPE,
    $input :: jsonb ->> 'id' AS value;

SELECT
    'Desambiguator' AS label,
    'desambiguator' AS name,
    $input :: jsonb ->> 'input_type' AS "type",
    $input :: jsonb ->> 'placeholder' AS placeholder,
    6 AS width
WHERE
    $input :: jsonb ->> 'id' = '1';

SELECT
    'Desambiguator' AS label,
    'desambiguator' AS name,
    $input :: jsonb ->> 'input_type' AS "type",
    $input :: jsonb ->> 'placeholder' AS placeholder,
    CURRENT_DATE AS value,
    6 AS width
WHERE
    $input :: jsonb ->> 'id' = '2';

-- ── Desambiguator Definitions End ───────────────────────────────────
SELECT
    'Atom' AS label,
    'atom' AS name,
    'FFF' AS placeholder,
    6 AS width;

SELECT
    'Description' AS label,
    'text' AS name,
    'What is this entry about?' AS placeholder,
    'textarea' AS "type";
