-- Phase 3 E2E Test: Branching Workflow
-- Test workflow: body analysis → logic (if relevanz = "decrease") → then/else paths

-- 1. Cleanup (use slug for lookup)
DELETE FROM workflow_executions WHERE workflow_id IN (
    SELECT id FROM workflow_definitions WHERE slug = 'phase3-e2e-branching'
);
DELETE FROM workflow_definitions WHERE slug = 'phase3-e2e-branching';

-- 2. Create test workflow
INSERT INTO workflow_definitions (name, slug, description, graph, active)
VALUES (
    'Phase 3 E2E Test - Branching',
    'phase3-e2e-branching',
    'Test workflow with logic node and conditional branching',
    '{
        "nodes": [
            {
                "id": "start",
                "type": "start",
                "position": {"x": 100, "y": 100}
            },
            {
                "id": "body_analysis",
                "type": "analysis",
                "prompt_slug": "pipeline_body",
                "question_augmentations": [
                    {
                        "id": "q1",
                        "type": "relevanz",
                        "question": "Hat sich die Fettmasse relevant verändert?",
                        "answer_spectrum": ["increase", "stable", "decrease"],
                        "reasoning_required": true
                    }
                ],
                "position": {"x": 100, "y": 200}
            },
            {
                "id": "logic_check",
                "type": "logic",
                "condition": {
                    "expression": {
                        "operator": "eq",
                        "ref": "body_analysis.relevanz",
                        "value": "decrease"
                    },
                    "then_path": "e3",
                    "else_path": "e4"
                },
                "fallback": {
                    "strategy": "default_path"
                },
                "position": {"x": 100, "y": 300}
            },
            {
                "id": "decrease_path",
                "type": "analysis",
                "prompt_slug": "pipeline_body",
                "position": {"x": 50, "y": 400}
            },
            {
                "id": "not_decrease_path",
                "type": "analysis",
                "prompt_slug": "pipeline_body",
                "position": {"x": 150, "y": 400}
            },
            {
                "id": "end",
                "type": "end",
                "position": {"x": 100, "y": 500}
            }
        ],
        "edges": [
            {"id": "e1", "from": "start", "to": "body_analysis"},
            {"id": "e2", "from": "body_analysis", "to": "logic_check"},
            {"id": "e3", "from": "logic_check", "to": "decrease_path", "label": "then"},
            {"id": "e4", "from": "logic_check", "to": "not_decrease_path", "label": "else"},
            {"id": "e5", "from": "decrease_path", "to": "end"},
            {"id": "e6", "from": "not_decrease_path", "to": "end"}
        ]
    }',
    true
);

-- 3. Verify workflow was created
SELECT
    id,
    name,
    slug,
    active,
    jsonb_array_length(graph->'nodes') as node_count,
    jsonb_array_length(graph->'edges') as edge_count
FROM workflow_definitions
WHERE slug = 'phase3-e2e-branching';
