-- Phase 4 E2E Test: Branching + Join Workflow
-- Test workflow mit 2 parallelen Pfaden, die wieder zusammengeführt werden

-- 1. Insert workflow definition
INSERT INTO workflow_definitions (
    id,
    name,
    description,
    graph,
    active,
    created_by
) VALUES (
    'phase4-join-test',
    'Phase 4 Join Test Workflow',
    'Test workflow with branching and join node',
    '{
        "nodes": [
            {
                "id": "start",
                "type": "start",
                "position": {"x": 100, "y": 100}
            },
            {
                "id": "body_analysis",
                "type": "analysis",
                "prompt_slug": "body",
                "position": {"x": 100, "y": 200},
                "question_augmentations": [
                    {
                        "id": "relevanz",
                        "type": "relevanz",
                        "question": "Ist eine Gewichtsveränderung relevant?",
                        "answer_spectrum": ["ja", "nein", "unklar"]
                    }
                ]
            },
            {
                "id": "decision_logic",
                "type": "logic",
                "position": {"x": 100, "y": 300},
                "condition": {
                    "type": "if",
                    "expression": {
                        "operator": "eq",
                        "ref": "body_analysis.relevanz",
                        "value": "ja"
                    }
                },
                "fallback": {
                    "strategy": "default_path"
                }
            },
            {
                "id": "path_a_nutrition",
                "type": "analysis",
                "prompt_slug": "nutrition",
                "position": {"x": 50, "y": 400},
                "question_augmentations": [
                    {
                        "id": "prioritaet",
                        "type": "prioritaet",
                        "question": "Wie wichtig ist Ernährungsoptimierung?",
                        "answer_spectrum": ["hoch", "mittel", "niedrig", "unklar"]
                    }
                ]
            },
            {
                "id": "path_b_activity",
                "type": "analysis",
                "prompt_slug": "activity",
                "position": {"x": 150, "y": 400},
                "question_augmentations": [
                    {
                        "id": "prioritaet",
                        "type": "prioritaet",
                        "question": "Wie wichtig ist Trainingsoptimierung?",
                        "answer_spectrum": ["hoch", "mittel", "niedrig", "unklar"]
                    }
                ]
            },
            {
                "id": "join_consolidation",
                "type": "join",
                "position": {"x": 100, "y": 500},
                "join_strategy": "best_effort",
                "skip_handling": "ignore_skipped"
            },
            {
                "id": "end",
                "type": "end",
                "position": {"x": 100, "y": 600}
            }
        ],
        "edges": [
            {"id": "e1", "from_node": "start", "to_node": "body_analysis"},
            {"id": "e2", "from_node": "body_analysis", "to_node": "decision_logic"},
            {"id": "e3", "from_node": "decision_logic", "to_node": "path_a_nutrition", "label": "then"},
            {"id": "e4", "from_node": "decision_logic", "to_node": "path_b_activity", "label": "else"},
            {"id": "e5", "from_node": "path_a_nutrition", "to_node": "join_consolidation"},
            {"id": "e6", "from_node": "path_b_activity", "to_node": "join_consolidation"},
            {"id": "e7", "from_node": "join_consolidation", "to_node": "end"}
        ]
    }'::jsonb,
    true,
    'test-user'
);

-- 2. Verify workflow was created
SELECT
    id,
    name,
    active,
    jsonb_array_length(graph->'nodes') as node_count,
    jsonb_array_length(graph->'edges') as edge_count
FROM workflow_definitions
WHERE id = 'phase4-join-test';

-- Expected result:
-- id: phase4-join-test
-- name: Phase 4 Join Test Workflow
-- active: true
-- node_count: 7
-- edge_count: 7
