Feature: Plan lifecycle CLI coverage
  As a developer
  I want to exercise v3 plan lifecycle CLI commands
  So that plan lifecycle branches in plan.py are covered

  Background:
    Given a plan lifecycle CLI runner
    And a mocked lifecycle service for plan commands

  Scenario: Plan use parses typed arguments
    When I run plan lifecycle use with parsed arguments
    Then the plan lifecycle command should succeed
    And the plan lifecycle use should pass parsed arguments
    And the plan lifecycle output should contain "Strategize phase"

  Scenario: Plan use rejects invalid argument format
    When I run plan lifecycle use with invalid argument "badarg"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Invalid argument format"

  Scenario: Plan use handles action not available errors
    When I run plan lifecycle use causing "action not available"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Action not available"

  Scenario: Plan use handles validation errors
    When I run plan lifecycle use causing "validation error"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Validation Error"

  Scenario: Plan use handles general errors
    When I run plan lifecycle use causing "general error"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Error"

  Scenario: Plan execute aborts when no plans are ready
    When I run plan execute without a plan id with 0 complete plans
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "No plans ready for execution"

  Scenario: Plan execute aborts when multiple plans are ready
    When I run plan execute without a plan id with 2 complete plans
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Multiple plans ready for execution"

  Scenario: Plan execute runs when one plan is ready
    When I run plan execute without a plan id with 1 complete plans
    Then the plan lifecycle command should succeed
    And the execute command should run the single ready plan
    And the plan lifecycle output should contain "Plan Executed"

  Scenario: Plan execute handles invalid phase transition
    When I run plan execute for plan id "01ARZ3NDEKTSV4RRFFQ69G5FAV" causing "invalid transition"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Invalid transition"

  Scenario: Plan execute handles plan not ready
    When I run plan execute for plan id "01ARZ3NDEKTSV4RRFFQ69G5FAW" causing "plan not ready"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Plan not ready"

  Scenario: Plan execute handles general errors
    When I run plan execute for plan id "01ARZ3NDEKTSV4RRFFQ69G5FAX" causing "general error"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Error"

  Scenario: Plan lifecycle apply aborts when no plans are ready
    When I run plan lifecycle apply without a plan id with 0 complete plans
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "No plans ready for apply"

  Scenario: Plan lifecycle apply aborts when multiple plans are ready
    When I run plan lifecycle apply without a plan id with 2 complete plans
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Multiple plans ready for apply"

  Scenario: Plan lifecycle apply runs when one plan is ready
    When I run plan lifecycle apply without a plan id with 1 complete plans
    Then the plan lifecycle command should succeed
    And the lifecycle apply command should run the single ready plan
    And the plan lifecycle output should contain "Apply phase"

  Scenario: Plan lifecycle apply handles invalid phase transition
    When I run plan lifecycle apply for plan id "01ARZ3NDEKTSV4RRFFQ69G5FAY" causing "invalid transition"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Invalid transition"

  Scenario: Plan lifecycle apply handles plan not ready
    When I run plan lifecycle apply for plan id "01ARZ3NDEKTSV4RRFFQ69G5FAZ" causing "plan not ready"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Plan not ready"

  Scenario: Plan lifecycle apply handles general errors
    When I run plan lifecycle apply for plan id "01ARZ3NDEKTSV4RRFFQ69G5FB0" causing "general error"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Error"

  Scenario: Plan status shows no plans
    When I run plan status without a plan id and 0 plans exist
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "No v3 lifecycle plans found"

  Scenario: Plan status lists active plans
    When I run plan status without a plan id and 2 plans exist
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Active Plans"

  Scenario: Plan status falls back for legacy plan
    When I run plan status for legacy plan id "01ARZ3NDEKTSV4RRFFQ69G5FB1"
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Plan: legacy-plan"

  Scenario: Plan status handles general errors
    When I run plan status causing a general error
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Error"

  Scenario: Plan lifecycle list rejects invalid phase
    When I run plan lifecycle list with invalid phase "notaphase"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Invalid phase"

  Scenario: Plan lifecycle list shows no plans
    When I run plan lifecycle list with no plans
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "No plans found"

  Scenario: Plan lifecycle list shows project summaries
    When I run plan lifecycle list with plans and phase "execute"
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Plans"
    And the plan lifecycle output should contain "proj-2"
    And the plan lifecycle output should contain "more"

  Scenario: Plan lifecycle list handles general errors
    When I run plan lifecycle list causing a general error
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Error"

  Scenario: Plan cancel reports cancellation with reason
    When I run plan cancel for plan id "01ARZ3NDEKTSV4RRFFQ69G5FB2" with reason "User requested"
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Plan cancelled"
    And the plan lifecycle output should contain "Reason: User requested"

  Scenario: Plan cancel reports cancellation without reason
    When I run plan cancel for plan id "01ARZ3NDEKTSV4RRFFQ69G5FB3" without reason
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Plan cancelled"
    And the plan lifecycle output should not contain "Reason:"

  Scenario: Plan cancel handles plan errors
    When I run plan cancel for plan id "01ARZ3NDEKTSV4RRFFQ69G5FB4" causing "plan error"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Cannot cancel"

  Scenario: Plan cancel handles general errors
    When I run plan cancel for plan id "01ARZ3NDEKTSV4RRFFQ69G5FB5" causing "general error"
    Then the plan lifecycle command should abort
    And the plan lifecycle output should contain "Error"

  Scenario: Plan execute shares lifecycle service instance with executor
    When I run plan execute verifying lifecycle service sharing
    Then the plan executor should receive the same lifecycle service instance

  # ---- _print_lifecycle_plan five-panel output ----

  Scenario: Plan status renders Plan Status panel with required fields
    When I run plan status for a plan with all five panels
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Plan Status"
    And the plan lifecycle output should contain "Processing State"
    And the plan lifecycle output should contain "Projects"
    And the plan lifecycle output should contain "Action"
    And the plan lifecycle output should contain "Phase"
    And the plan lifecycle output should contain "Attempt"

  Scenario: Plan status renders Progress panel
    When I run plan status for a plan with all five panels
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Progress"
    And the plan lifecycle output should contain "Strategize"
    And the plan lifecycle output should contain "Execute"
    And the plan lifecycle output should contain "Apply"

  Scenario: Plan status renders Timing panel
    When I run plan status for a plan with all five panels
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Timing"
    And the plan lifecycle output should contain "Started"
    And the plan lifecycle output should contain "Elapsed"
    And the plan lifecycle output should contain "ETA"

  Scenario: Plan status renders Execution Detail panel
    When I run plan status for a plan with all five panels
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Execution Detail"
    And the plan lifecycle output should contain "Sandbox"
    And the plan lifecycle output should contain "Tool Calls"
    And the plan lifecycle output should contain "Files Modified"
    And the plan lifecycle output should contain "Child Plans"
    And the plan lifecycle output should contain "Checkpoints"

  Scenario: Plan status renders Cost panel
    When I run plan status for a plan with all five panels
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Cost"
    And the plan lifecycle output should contain "Tokens Used"
    And the plan lifecycle output should contain "Cost So Far"
    And the plan lifecycle output should contain "Estimated"

  Scenario: Plan status renders status refresh footer
    When I run plan status for a plan with all five panels
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Status refreshed"

  Scenario: Plan status renders Arguments when plan has arguments
    When I run plan status for a plan with arguments
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Arguments"
    And the plan lifecycle output should contain "coverage"

  Scenario: Plan status renders Automation Profile when set
    When I run plan status for a plan with automation profile
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Automation Profile"
    And the plan lifecycle output should contain "trusted"

  Scenario: Plan status renders Estimation Actor when set
    When I run plan status for a plan with estimation actor
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Estimation Actor"
    And the plan lifecycle output should contain "openai/gpt-4"

  Scenario: Plan status renders Invariant Actor when set
    When I run plan status for a plan with invariant actor
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Invariant Actor"
    And the plan lifecycle output should contain "openai/gpt-4"

  Scenario: Plan status renders phase timestamps when populated
    When I run plan status for a plan with all timestamps
    Then the plan lifecycle command should succeed
    And the plan lifecycle output should contain "Strategize Started"
    And the plan lifecycle output should contain "Strategize Completed"
    And the plan lifecycle output should contain "Execute Started"
    And the plan lifecycle output should contain "Execute Completed"
    And the plan lifecycle output should contain "Applied At"
