digraph Solitaire {
    graph [
        goal="Build a terminal-based solitaire (Klondike) game in Python",
        rankdir=LR,
        default_max_retries=3,
        retry_target="impl_setup",
        fallback_retry_target="impl_logic"
    ]

    start [shape=Mdiamond, label="Start"]
    exit  [shape=Msquare, label="Exit"]

    expand_spec [
        label="Expand Spec",
        shape=box,
        prompt="Goal: $goal

Create a detailed implementation spec for a Python terminal solitaire game.

Cover:
- Game rules and data structures (Card, Deck, Pile types)
- Terminal rendering approach using the standard-library curses module
- Input handling and move validation
- Win/loss detection
- UI layout
- Test strategy

Keep game rules testable without curses. Include a smoke mode so `python3 main.py --smoke` starts enough of the app to prove imports and setup without requiring an interactive terminal.

Write the spec to .ai/solitaire-spec.md.
Write status.json at workspace root: outcome=succeeded if the spec is complete, outcome=failed with failure_reason otherwise."
    ]

    impl_setup [
        label="Setup Project",
        shape=box,
        prompt="Read .ai/solitaire-spec.md.

Create the Python project skeleton under solitaire-app/:
- pyproject.toml with pytest configured
- main.py entrypoint
- src/solitaire_tui/ package
- tests/ directory
- README.md stub

Add minimal importable modules so the project compiles.

Run:
cd solitaire-app && python3 -m py_compile main.py src/solitaire_tui/*.py

Write status.json at workspace root: outcome=succeeded if the project skeleton exists and compiles, outcome=failed with failure_reason otherwise."
    ]

    verify_setup [
        label="Verify Setup",
        shape=box,
        class="verify",
        prompt="Verify setup for the solitaire app.

Check:
1. solitaire-app/pyproject.toml exists
2. solitaire-app/main.py exists
3. solitaire-app/src/solitaire_tui exists
4. Python files compile

Run:
cd solitaire-app && python3 -m py_compile main.py src/solitaire_tui/*.py

Write findings to .ai/verify_setup.md.
Write status.json at workspace root: outcome=succeeded if all checks pass, outcome=failed with failure_reason otherwise."
    ]

    check_setup [shape=diamond, label="Setup OK?"]

    impl_data [
        label="Data Structures",
        shape=box,
        prompt="Read .ai/solitaire-spec.md.

Implement Card, Deck, and Pile types under solitaire-app/src/solitaire_tui/.

Add focused unit tests under solitaire-app/tests/.

Run:
cd solitaire-app && python3 -m pytest tests/ -v

Write status.json at workspace root: outcome=succeeded if tests pass and the data model is implemented, outcome=failed with failure_reason otherwise."
    ]

    verify_data [
        label="Verify Data",
        shape=box,
        class="verify",
        prompt="Verify the solitaire data structures.

Run:
cd solitaire-app && python3 -m pytest tests/ -v && python3 -m py_compile main.py src/solitaire_tui/*.py

Check that Card, Deck, and Pile types are defined and basic operations work.

Write findings to .ai/verify_data.md.
Write status.json at workspace root: outcome=succeeded if all checks pass, outcome=failed with failure_reason otherwise."
    ]

    check_data [shape=diamond, label="Data OK?"]

    impl_logic [
        label="Game Logic",
        shape=box,
        class="hard",
        max_retries=2,
        prompt="Read .ai/solitaire-spec.md and the current solitaire-app implementation.

Implement Klondike rules:
- Initial deal
- Move validation
- Auto-complete detection
- Win condition
- Undo

Add tests for legal moves, illegal moves, win detection, and edge cases.

Run:
cd solitaire-app && python3 -m pytest tests/ -v

Write status.json at workspace root: outcome=succeeded if all tests pass and rules are implemented, outcome=failed with failure_reason otherwise."
    ]

    verify_logic [
        label="Verify Logic",
        shape=box,
        class="verify",
        prompt="Verify Klondike game logic.

Run:
cd solitaire-app && python3 -m pytest tests/ -v

Check move validation, win detection, and undo.

Write findings to .ai/verify_logic.md.
Write status.json at workspace root: outcome=succeeded if all checks pass, outcome=failed with failure_reason otherwise."
    ]

    check_logic [shape=diamond, label="Logic OK?"]

    impl_ui [
        label="Terminal UI",
        shape=box,
        class="hard",
        max_retries=2,
        prompt="Read .ai/solitaire-spec.md and the game logic.

Implement the curses TUI:
- Card rendering using ASCII art
- Board layout
- Keyboard input
- Move selection
- Help text
- `python3 main.py --smoke` non-interactive smoke path

Keep rendering helpers testable where practical and avoid coupling game rules to curses.

Run:
cd solitaire-app && python3 -m pytest tests/ -v && python3 -m py_compile main.py src/solitaire_tui/*.py && python3 main.py --smoke

Write status.json at workspace root: outcome=succeeded if tests pass, files compile, and smoke mode works, outcome=failed with failure_reason otherwise."
    ]

    verify_ui [
        label="Verify UI",
        shape=box,
        class="verify",
        prompt="Verify the terminal UI.

Run:
cd solitaire-app && python3 -m pytest tests/ -v && python3 -m py_compile main.py src/solitaire_tui/*.py && python3 main.py --smoke

Check that:
- main.py can start smoke mode
- UI module imports without requiring an interactive terminal
- Board rendering helpers have tests or smoke coverage
- Controls are documented in README.md

Write findings to .ai/verify_ui.md.
Write status.json at workspace root: outcome=succeeded if all checks pass, outcome=failed with failure_reason otherwise."
    ]

    check_ui [shape=diamond, label="UI OK?"]

    impl_integration [
        label="Integrate",
        shape=box,
        prompt="Finish the solitaire app.

Do the integration work:
- Wire main.py to start the curses game loop normally
- Keep --smoke non-interactive
- Add README.md run instructions and controls
- Add any missing tests needed for confidence
- Ensure no generated files are outside solitaire-app/ except .ai/ reports and root status.json

Run:
cd solitaire-app && python3 -m pytest tests/ -v && python3 main.py --smoke

Write status.json at workspace root: outcome=succeeded if the app is playable and tests pass, outcome=failed with failure_reason otherwise."
    ]

    verify_integration [
        label="Verify Integration",
        shape=box,
        class="verify",
        prompt="Verify final integration.

Run:
cd solitaire-app && python3 -m pytest tests/ -v && python3 -m py_compile main.py src/solitaire_tui/*.py && python3 main.py --smoke

Check README.md includes setup, run, test, and controls instructions.

Write findings to .ai/verify_integration.md.
Write status.json at workspace root: outcome=succeeded if all checks pass, outcome=failed with failure_reason otherwise."
    ]

    check_integration [shape=diamond, label="Integration OK?"]

    review [
        label="Final Review",
        shape=box,
        class="hard",
        goal_gate=true,
        prompt="Review the complete solitaire app against .ai/solitaire-spec.md.

Confirm:
- The app is in solitaire-app/
- It is Python 3.11+ and uses curses for the TUI
- Klondike rules are implemented correctly
- Keyboard controls are usable and documented
- Tests pass
- Smoke mode works without an interactive terminal

Run:
cd solitaire-app && python3 -m pytest tests/ -v && python3 main.py --smoke

Write review to .ai/solitaire-review.md.
Write status.json at workspace root: outcome=succeeded if the app is complete and demo-ready, outcome=failed with specific missing or broken items."
    ]

    check_review [shape=diamond, label="Review OK?"]

    start -> expand_spec -> impl_setup -> verify_setup -> check_setup

    check_setup -> impl_data  [condition="outcome=succeeded"]
    check_setup -> impl_setup [condition="outcome=failed", label="Retry"]
    check_setup -> impl_setup [label="Fallback"]

    impl_data -> verify_data -> check_data

    check_data -> impl_logic [condition="outcome=succeeded"]
    check_data -> impl_data  [condition="outcome=failed", label="Retry"]
    check_data -> impl_data  [label="Fallback"]

    impl_logic -> verify_logic -> check_logic

    check_logic -> impl_ui    [condition="outcome=succeeded"]
    check_logic -> impl_logic [condition="outcome=failed", label="Retry"]
    check_logic -> impl_logic [label="Fallback"]

    impl_ui -> verify_ui -> check_ui

    check_ui -> impl_integration [condition="outcome=succeeded"]
    check_ui -> impl_ui          [condition="outcome=failed", label="Retry"]
    check_ui -> impl_ui          [label="Fallback"]

    impl_integration -> verify_integration -> check_integration

    check_integration -> review           [condition="outcome=succeeded"]
    check_integration -> impl_integration [condition="outcome=failed", label="Retry"]
    check_integration -> impl_integration [label="Fallback"]

    review -> check_review

    check_review -> exit    [condition="outcome=succeeded"]
    check_review -> impl_ui [condition="outcome=failed", label="Fix"]
    check_review -> impl_ui [label="Fallback"]
}
