"""Wizard branching: a step's next_step_rules jump ahead or end the form,
and the steps the answers don't lead to aren't asked — the server works out
the same path as the browser (app/core/form_validation.py::wizard_unreached,
frontend lib/wizardPath.ts)."""

import uuid

from app.core.form_validation import missing_required_fields, visible_fields

FIELDS = [
    {"name": "w_check", "type": "wizard", "label": "Eligibility", "next_step_rules": [{"when": "eligible == 'No'", "goto": "end"}, {"when": "fast_track == true", "goto": "w_declare"}]},
    {"name": "eligible", "type": "radio", "label": "Eligible", "options": ["Yes", "No"], "required": True},
    {"name": "fast_track", "type": "checkbox", "label": "Fast track"},
    {"name": "w_income", "type": "wizard", "label": "Income"},
    {"name": "income", "type": "number", "label": "Income", "required": True},
    {"name": "w_docs", "type": "wizard", "label": "Documents", "visible_when": "income > 100000"},
    {"name": "proof", "type": "text", "label": "Proof", "required": True},
    {"name": "w_declare", "type": "wizard", "label": "Declaration"},
    {"name": "agree", "type": "consent", "label": "Agree", "required": True},
]


def test_path_follows_the_answers():
    assert missing_required_fields(FIELDS, {"eligible": "No"}) == []
    assert missing_required_fields(FIELDS, {"eligible": "Yes", "fast_track": True}) == ["agree"]
    assert missing_required_fields(FIELDS, {"eligible": "Yes", "income": 50000}) == ["agree"]
    assert missing_required_fields(FIELDS, {"eligible": "Yes", "income": 500000}) == ["proof", "agree"]
    visible = visible_fields(FIELDS, {"eligible": "Yes", "fast_track": True})
    assert not visible["income"] and not visible["w_income"] and visible["agree"]


def test_backwards_and_unknown_jumps_are_ignored():
    fields = [
        {"name": "w1", "type": "wizard", "label": "One"},
        {"name": "a", "type": "text", "label": "A", "required": True},
        {"name": "w2", "type": "wizard", "label": "Two", "next_step_rules": [{"when": "true", "goto": "w1"}, {"when": "true", "goto": "nowhere"}]},
        {"name": "w3", "type": "wizard", "label": "Three"},
        {"name": "c", "type": "text", "label": "C", "required": True},
    ]
    assert missing_required_fields(fields, {}) == ["a", "c"]


async def test_submission_skips_unreached_steps(client, auth_headers):
    created = await client.post("/forms", headers=auth_headers, json={"name": f"Pytest Wizard {uuid.uuid4().hex[:6]}", "field_definitions": FIELDS})
    assert created.status_code == 201, created.text
    form = (await client.post(f"/forms/{created.json()['form_id']}/publish", headers=auth_headers)).json()
    ended = await client.post("/form-submissions", headers=auth_headers, json={"form_id": form["form_id"], "form_version": form["version"], "field_values": {"eligible": "No"}})
    assert ended.status_code == 201, ended.text
    short = await client.post(
        "/form-submissions", headers=auth_headers, json={"form_id": form["form_id"], "form_version": form["version"], "field_values": {"eligible": "Yes", "fast_track": True}}
    )
    assert short.status_code == 422 and short.json()["detail"]["missing_required_fields"] == ["agree"]

    bad = await client.post(
        "/forms",
        headers=auth_headers,
        json={"name": "x", "field_definitions": [{"name": "t", "type": "text", "label": "T", "next_step_rules": [{"when": "true", "goto": "end"}]}]},
    )
    assert bad.status_code == 422
