Feature: Plan Resume
  As a developer
  I want to resume interrupted or errored plan executions
  So that I don't lose progress when something goes wrong

  Background:
    Given I have a plan lifecycle service for resume tests
    And I have a plan resume service

  # Resume eligibility validation

  Scenario: Eligible plan in errored state can be resumed
    Given I have a resume-test plan in execute phase with errored state
    When I validate resume eligibility
    Then the plan should be eligible for resume

  Scenario: Eligible plan in processing state can be resumed
    Given I have a resume-test plan in execute phase with processing state
    When I validate resume eligibility
    Then the plan should be eligible for resume

  Scenario: Eligible plan in queued state can be resumed
    Given I have a resume-test plan in strategize phase with queued state
    When I validate resume eligibility
    Then the plan should be eligible for resume

  Scenario: Terminal applied plan cannot be resumed
    Given I have a resume-test plan in applied terminal state
    When I validate resume eligibility
    Then the plan should not be eligible for resume
    And the ineligible reason should be "terminal_applied"

  Scenario: Terminal cancelled plan cannot be resumed
    Given I have a resume-test plan in cancelled terminal state
    When I validate resume eligibility
    Then the plan should not be eligible for resume
    And the ineligible reason should be "terminal_cancelled"

  Scenario: Terminal constrained plan cannot be resumed
    Given I have a resume-test plan in constrained terminal state
    When I validate resume eligibility
    Then the plan should not be eligible for resume
    And the ineligible reason should be "terminal_constrained"

  Scenario: Action phase plan cannot be resumed
    Given I have a resume-test plan in action phase
    When I validate resume eligibility
    Then the plan should not be eligible for resume
    And the ineligible reason should be "action_phase"

  # Resume metadata and checkpoints

  Scenario: Record step checkpoint during execution
    Given I have a resume-test plan in execute phase with processing state
    When I record a resume step checkpoint at index 0 with decision "DEC001"
    Then the resume-test plan should have last completed step 0
    And the resume-test plan should have a checkpoint ID set

  Scenario: Record multiple checkpoints
    Given I have a resume-test plan in execute phase with processing state
    When I record a resume step checkpoint at index 0 with decision "DEC001"
    And I record a resume step checkpoint at index 1 with decision "DEC002"
    And I record a resume step checkpoint at index 2 with decision "DEC003"
    Then the resume-test plan should have last completed step 2
    And the resume metadata should have 3 checkpoints

  Scenario: Resume metadata tracks next step correctly
    Given I have a resume-test plan in execute phase with processing state
    When I record a resume step checkpoint at index 2 with decision "DEC003"
    Then the resume metadata next step should be 3

  # Resume summary (dry-run)

  Scenario: Dry-run shows resume point without changing state
    Given I have a resume-test plan in execute phase with errored state
    And the resume-test plan has checkpoint at step 2 with decision "DEC003"
    When I resume the plan with dry-run
    Then I should get a resume summary
    And the resume summary next step should be 3
    And the resume-test plan processing state should still be errored

  Scenario: Resume summary includes decision ID from checkpoint
    Given I have a resume-test plan in execute phase with errored state
    And the resume-test plan has checkpoint at step 1 with decision "DEC002"
    When I resume the plan with dry-run
    Then the resume summary decision ID should be "DEC002"

  # Live resume

  Scenario: Resume errored plan resets to processing
    Given I have a resume-test plan in execute phase with errored state
    And the resume-test plan has checkpoint at step 1 with decision "DEC002"
    When I resume the plan without dry-run
    Then the resume-test plan processing state should be processing
    And the resume-test plan error message should be cleared

  Scenario: Resume queued plan transitions to processing
    Given I have a resume-test plan in strategize phase with queued state
    When I resume the plan without dry-run
    Then the resume-test plan processing state should be processing

  # Graceful shutdown

  Scenario: Record graceful shutdown marks interrupted
    Given I have a resume-test plan in execute phase with processing state
    When I record a resume step checkpoint at index 1 with decision "DEC002"
    And I record a graceful shutdown for resume
    Then the resume metadata should be marked as interrupted

  # Error handling

  Scenario: Resume with empty plan ID raises validation error
    When I try to resume a plan with empty ID
    Then a resume validation error should be raised

  Scenario: Validate eligibility with empty plan ID raises error
    When I try to validate eligibility with empty plan ID
    Then a resume validation error should be raised

  Scenario: Resume terminal plan raises plan error
    Given I have a resume-test plan in applied terminal state
    When I try to resume the terminal plan for resume test
    Then a plan error should be raised for resume

  # Step count tracking

  Scenario: Set total steps for a plan
    Given I have a resume-test plan in execute phase with processing state
    When I set the resume total steps to 5
    Then the resume metadata total steps should be 5

  Scenario: Set total steps with invalid values raises error
    When I try to set resume total steps with empty plan ID
    Then a resume validation error should be raised

  # ResumeMetadata model properties

  Scenario: Resume metadata has_progress when steps completed
    Given I have resume metadata with last completed step 2
    Then the resume metadata has_progress should be true

  Scenario: Resume metadata has no progress initially
    Given I have fresh resume metadata
    Then the resume metadata has_progress should be false

  Scenario: Resume metadata is_complete when all steps done
    Given I have resume metadata with 3 total steps and last step 2
    Then the resume metadata is_complete should be true

  Scenario: Resume metadata is not complete with remaining steps
    Given I have resume metadata with 5 total steps and last step 2
    Then the resume metadata is_complete should be false
