@phase2 @plan @cli @rollback
Feature: Plan rollback CLI command
  As a user
  I want to list available checkpoints and restore a plan to a checkpoint via CLI
  So that I can recover from failed plan executions

  # ───────────────────────────────────────────────────────────
  # List mode scenarios (agents plan rollback)
  # ───────────────────────────────────────────────────────────

  Scenario: List checkpoints for a plan
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has 3 checkpoints
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV"
    Then the output should list 3 checkpoints
    And each checkpoint should show ID, timestamp, and metadata

  Scenario: List checkpoints with empty list
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has no checkpoints
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV"
    Then the output should indicate no checkpoints available

  Scenario: List checkpoints with rich output format
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has 2 checkpoints
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV --format rich"
    Then the output should be formatted as a rich table
    And the table should contain checkpoint IDs and metadata

  Scenario: List checkpoints with JSON output format
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has 2 checkpoints
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV --format json"
    Then the rollback output should be valid JSON
    And the JSON should contain a list of checkpoints

  Scenario: List checkpoints with YAML output format
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has 2 checkpoints
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV --format yaml"
    Then the rollback output should be valid YAML
    And the YAML should contain a list of checkpoints

  Scenario: List checkpoints with plain output format
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has 2 checkpoints
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV --format plain"
    Then the output should be plain text
    And the text should contain checkpoint IDs

  # ───────────────────────────────────────────────────────────
  # Restore mode scenarios (agents plan rollback <id> <checkpoint-id>)
  # ───────────────────────────────────────────────────────────

  Scenario: Restore plan to checkpoint with confirmation
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV" with confirmation
    Then the plan rollback should succeed
    And the output should show rollback summary

  Scenario: Restore plan to checkpoint with --yes flag
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV --yes"
    Then the plan rollback should succeed
    And no rollback confirmation prompt should be shown

  Scenario: Restore plan to checkpoint with --to-checkpoint option
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV --to-checkpoint 01BRZ4NFEKTSV4RRFFQ69G5FAV --yes"
    Then the plan rollback should succeed

  Scenario: Restore plan to non-existent checkpoint
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01NONEXISTENT0000000000000 --yes"
    Then the command should fail with not found error

  Scenario: Restore applied plan fails
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan is marked as applied
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV --yes"
    Then the command should fail with "plan is already applied"

  Scenario: Restore plan without checkpoint ID shows error
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV"
    Then the output should list available checkpoints

  Scenario: Restore plan with JSON output format
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV --yes --format json"
    Then the rollback output should be valid JSON
    And the JSON should contain rollback summary

  Scenario: Restore plan with YAML output format
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV --yes --format yaml"
    Then the rollback output should be valid YAML
    And the YAML should contain rollback summary

  # ───────────────────────────────────────────────────────────
  # Atomic rollback verification
  # ───────────────────────────────────────────────────────────

  Scenario: Rollback is atomic - no partial state on failure
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV --yes"
    Then the rollback should be atomic
    And the plan state should be consistent

  Scenario: Rollback shows restored files count
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV --yes"
    Then the output should show the number of restored files

  Scenario: Rollback shows changed paths
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV --yes"
    Then the output should list changed paths

  # ───────────────────────────────────────────────────────────
  # Error handling scenarios
  # ───────────────────────────────────────────────────────────

  Scenario: Rollback with invalid plan ID
    When I run "agents plan rollback INVALID_PLAN_ID"
    Then the command should fail with validation error

  Scenario: Rollback with missing plan
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV"
    Then the command should fail with not found error

  Scenario: Rollback cancelled by user
    Given a plan "01ARZ3NDEKTSV4RRFFQ69G5FAV" exists
    And the plan has a checkpoint "01BRZ4NFEKTSV4RRFFQ69G5FAV"
    And the plan is not applied
    When I run "agents plan rollback 01ARZ3NDEKTSV4RRFFQ69G5FAV 01BRZ4NFEKTSV4RRFFQ69G5FAV" and cancel confirmation
    Then the rollback should be cancelled
    And the plan state should remain unchanged
