Feature: Plan Lifecycle Service
  As a developer
  I want a service that manages the v3 plan lifecycle
  So that plans can transition through Strategize -> Execute -> Apply phases

  Background:
    Given I have a plan lifecycle service

  # Action Creation Tests

  Scenario: Create a new action
    When I create an action with name "local/test-coverage" and definition "Tests pass"
    Then the action should be created successfully
    And the lifecycle action state should be "available"
    And the lifecycle action namespace should be "local"

  Scenario: Create action with arguments
    When I create an action with name "local/coverage" definition "Coverage >= target" and arguments
      | name    | type | requirement | description         |
      | target  | int  | required    | Target percentage   |
    Then the action should be created successfully
    And the action should have 1 argument

  Scenario: Reject invalid action name
    When I try to create an action with invalid name "local/invalid name"
    Then a lifecycle validation error should be raised

  Scenario: Action is available immediately after creation
    Given I have an available action "local/my-action"
    Then the lifecycle action state should be "available"

  Scenario: Archive an action
    Given I have an available action "local/my-action"
    When I archive the action
    Then the lifecycle action state should be "archived"

  Scenario: Get action by ID
    Given I have created an action "local/test-action"
    When I get the action by ID
    Then the action should be returned

  Scenario: Get action by name
    Given I have created an action "local/test-action"
    When I get the action by name "local/test-action"
    Then the action should be returned

  Scenario: Get action by missing ID
    Given I have created an action "local/test-action"
    When I try to get the action by missing ID "missing-action-id"
    Then a not found error should be raised

  Scenario: Get action by missing name
    Given I have created an action "local/test-action"
    When I try to get the action by missing name "local/missing-action"
    Then a not found error should be raised

  Scenario: List actions
    Given I have created actions "local/action-a" and "local/action-b"
    When I list all actions
    Then I should see 2 actions

  Scenario: List actions by namespace
    Given I have created actions "local/action-a" and "myorg/action-b"
    When I list actions in namespace "local"
    Then I should see 1 action

  Scenario: List actions by state
    Given I have an available action "local/available-action"
    And I have an archived action "local/archived-action"
    When I list actions in state "available"
    Then I should see 1 action

  Scenario: Make available on already available action is a no-op
    Given I have an available action "local/available-action"
    When I try to make the action available
    Then the lifecycle action state should be "available"

  Scenario: Get plan by missing ID
    When I try to get the plan by missing ID "missing-plan-id"
    Then a not found error should be raised

  # Plan Creation (use_action) Tests

  Scenario: Use action creates plan in Strategize phase
    Given I have an available action "local/test-action"
    When I use the action on project "project-123"
    Then a plan should be created
    And the lifecycle plan phase should be "strategize"
    And the lifecycle plan processing state should be "queued"
    And the plan should reference project "project-123"

  Scenario: Use action validates required arguments
    Given I have an available action with required argument "target:int:required:Target"
    When I try to use the action without providing arguments
    Then a validation error should be raised with message "Missing required argument"

  Scenario: Cannot use archived action before available
    Given I have an archived action "local/my-action"
    When I try to use the action on project "project-123"
    Then an action not available error should be raised

  Scenario: Cannot use archived action (second)
    Given I have an archived action "local/my-action"
    When I try to use the action on project "project-123"
    Then an action not available error should be raised

  Scenario: Non-reusable action is archived after use
    Given I have an available non-reusable action "local/one-time-action"
    When I use the action on project "project-123"
    Then the lifecycle action state should be "archived"

  # Strategize Phase Tests

  Scenario: Start strategize processing
    Given I have a plan in strategize phase with queued state
    When I start strategize
    Then the lifecycle plan processing state should be "processing"
    And the strategize started timestamp should be set

  Scenario: Complete strategize
    Given I have a plan in strategize phase with processing state
    When I complete strategize
    Then the lifecycle plan processing state should be "complete"
    And the strategize completed timestamp should be set

  Scenario: Fail strategize
    Given I have a plan in strategize phase with processing state
    When strategize fails with error "Strategy generation failed"
    Then the lifecycle plan processing state should be "errored"
    And the lifecycle plan error message should be "Strategy generation failed"

  Scenario: Cannot start strategize outside strategize phase
    Given I have a plan in execute phase with queued state
    When I try to start strategize
    Then a plan error should be raised

  Scenario: Cannot start strategize when not queued
    Given I have a plan in strategize phase with processing state
    When I try to start strategize
    Then a plan not ready error should be raised

  Scenario: Cannot complete strategize outside strategize phase
    Given I have a plan in execute phase with queued state
    When I try to complete strategize
    Then a plan error should be raised

  Scenario: Cannot complete strategize when not processing
    Given I have a plan in strategize phase with queued state
    When I try to complete strategize
    Then a plan error should be raised

  # Execute Phase Transition Tests

  Scenario: Execute plan transitions from Strategize to Execute
    Given I have a plan in strategize phase with complete state
    When I execute the plan
    Then the lifecycle plan phase should be "execute"
    And the lifecycle plan processing state should be "queued"

  Scenario: Cannot execute plan that is not complete
    Given I have a plan in strategize phase with processing state
    When I try to execute the plan
    Then a plan not ready error should be raised

  Scenario: Start execute processing
    Given I have a plan in execute phase with queued state
    When I start execute
    Then the lifecycle plan processing state should be "processing"
    And the execute started timestamp should be set

  Scenario: Complete execute
    Given I have a plan in execute phase with processing state
    When I complete execute
    Then the lifecycle plan processing state should be "complete"
    And the execute completed timestamp should be set

  Scenario: Cannot start execute outside execute phase
    Given I have a plan in strategize phase with complete state
    When I try to start execute
    Then a plan error should be raised

  Scenario: Cannot start execute when not queued
    Given I have a plan in execute phase with complete state
    When I try to start execute
    Then a plan not ready error should be raised

  Scenario: Cannot complete execute outside execute phase
    Given I have a plan in strategize phase with complete state
    When I try to complete execute
    Then a plan error should be raised

  Scenario: Cannot complete execute when not processing
    Given I have a plan in execute phase with queued state
    When I try to complete execute
    Then a plan error should be raised

  Scenario: Fail execute
    Given I have a plan in execute phase with processing state
    When execute fails with error "Execution failed"
    Then the lifecycle plan processing state should be "errored"
    And the lifecycle plan error message should be "Execution failed"

  # Apply Phase Transition Tests

  Scenario: Apply plan transitions from Execute to Apply
    Given I have a plan in execute phase with complete state
    When I lifecycle apply the plan
    Then the lifecycle plan phase should be "apply"
    And the lifecycle plan processing state should be "queued"

  Scenario: Cannot apply plan that is not complete
    Given I have a plan in execute phase with processing state
    When I try to lifecycle apply the plan
    Then a plan not ready error should be raised

  Scenario: Complete apply transitions to applied processing state (terminal)
    Given I have a plan in apply phase with processing state
    When I complete apply
    Then the lifecycle plan phase should be "apply"
    And the lifecycle plan processing state should be "applied"
    And the applied timestamp should be set
    And the plan should be terminal

  Scenario: Cannot start apply outside apply phase
    Given I have a plan in execute phase with complete state
    When I try to start apply
    Then a plan error should be raised

  Scenario: Cannot start apply when not queued
    Given I have a plan in apply phase with processing state
    When I try to start apply
    Then a plan not ready error should be raised

  Scenario: Cannot complete apply outside apply phase
    Given I have a plan in execute phase with complete state
    When I try to complete apply
    Then a plan error should be raised

  Scenario: Cannot complete apply when not processing
    Given I have a plan in apply phase with queued state
    When I try to complete apply
    Then a plan error should be raised

  Scenario: Fail apply
    Given I have a plan in apply phase with processing state
    When apply fails with error "Apply failed"
    Then the lifecycle plan processing state should be "errored"
    And the lifecycle plan error message should be "Apply failed"

  # Invalid Transition Tests


  Scenario: Cannot apply plan in Strategize phase
    Given I have a plan in strategize phase with complete state
    When I try to lifecycle apply the plan
    Then an invalid phase transition error should be raised

  Scenario: Invalid phase transition error accepts custom message
    When I create an invalid phase transition error from "strategize" to "execute" with message "Custom transition message"
    Then the invalid phase transition error should include message "Custom transition message"
    And the invalid transition error should reference from phase "strategize" and to phase "execute"

  # Cancel Tests

  Scenario: Cancel plan in Strategize phase
    Given I have a plan in strategize phase with processing state
    When I cancel the plan with reason "User requested cancellation"
    Then the lifecycle plan processing state should be "cancelled"
    And the lifecycle plan error message should be "User requested cancellation"

  Scenario: Cannot cancel terminal plan
    Given I have a plan in terminal applied state
    When I try to cancel the plan
    Then a plan error should be raised

  # List Plans Tests

  Scenario: List plans by phase
    Given I have plans in different phases
    When I list plans in execute phase
    Then I should see only execute phase plans

  Scenario: List plans by project
    Given I have plans for different projects
    When I list plans for project "project-123"
    Then I should see only plans for that project

  Scenario: List plans by namespace
    Given I have plans in different namespaces
    When I list plans in namespace "local"
    Then I should see 1 plan
