Feature: Action persistence via ActionRepository
  As a developer
  I want actions to persist correctly through the ActionRepository
  So that action definitions are durable across operations

  Background:
    Given a fresh in-memory action persistence database

  # Create scenarios
  Scenario: Create an action and retrieve it by name
    Given a new action with namespaced name "local/persist-test"
    When I persist the action via the action repository
    Then I can retrieve the action by name "local/persist-test"
    And the retrieved action description should be "Persistence test action"

  Scenario: Create an action preserves all core fields
    Given a fully-populated action "local/full-persist"
    When I persist the action via the action repository
    Then I can retrieve the action by name "local/full-persist"
    And the action strategy_actor should be "local/strategist"
    And the action execution_actor should be "local/executor"
    And the persisted action state should be "available"
    And the action reusable flag should be true

  # List and archive scenarios
  Scenario: List actions returns all available actions
    Given 3 persisted actions in available state
    When I list available actions from the action repository
    Then I should get 3 actions in the action list

  Scenario: List actions filters by namespace
    Given a persisted action "local/ns-one" in available state
    And a persisted action "myorg/ns-two" in available state
    When I list actions in namespace "local" from the action repository
    Then I should get 1 action in the action list

  Scenario: Archive action changes state to archived
    Given a persisted action "local/to-archive" in available state
    When I archive the action "local/to-archive" via the action repository
    Then the action "local/to-archive" should have state "archived"

  Scenario: Delete action removes it from the database
    Given a persisted action "local/to-delete" in available state
    When I delete the action "local/to-delete" via the action repository
    Then the action "local/to-delete" should not exist

  # Arguments ordering scenarios
  Scenario: Action arguments preserve position ordering
    Given an action "local/ordered-args" with arguments "alpha" at position 0 and "beta" at position 1 and "gamma" at position 2
    When I persist the action via the action repository
    And I retrieve the action "local/ordered-args" from the action repository
    Then the persisted action should have 3 arguments
    And the persisted argument at position 0 should be "alpha"
    And the persisted argument at position 1 should be "beta"
    And the persisted argument at position 2 should be "gamma"

  # Invariants ordering scenarios
  Scenario: Action invariants preserve position ordering
    Given an action "local/ordered-invs" with invariants "No deletion" and "Read only access" and "Log all changes"
    When I persist the action via the action repository
    And I retrieve the action "local/ordered-invs" from the action repository
    Then the persisted action should have 3 invariants
    And the persisted invariant at position 0 should be "No deletion"
    And the persisted invariant at position 1 should be "Read only access"
    And the persisted invariant at position 2 should be "Log all changes"

  # Action phase persistence / terminal state storage
  Scenario: Action persists in available state by default
    Given a new action with namespaced name "local/default-state"
    When I persist the action via the action repository
    Then the action "local/default-state" should have state "available"

  Scenario: Action persists archived state
    Given a new action "local/archived-action" in archived state
    When I persist the action via the action repository
    Then the action "local/archived-action" should have state "archived"

  # Apply terminal state storage for plans created from this action
  Scenario: Plan from action records applied terminal state
    Given a persisted action "local/term-action" in available state
    And a plan from action "local/term-action" in apply phase with state "applied"
    When I retrieve the terminal plan from the persistence database
    Then the terminal plan processing_state should be "applied"

  Scenario: Plan from action records constrained terminal state
    Given a persisted action "local/constr-action" in available state
    And a plan from action "local/constr-action" in apply phase with state "constrained"
    When I retrieve the terminal plan from the persistence database
    Then the terminal plan processing_state should be "constrained"

  Scenario: Plan from action records errored terminal state
    Given a persisted action "local/err-action" in available state
    And a plan from action "local/err-action" in apply phase with state "errored"
    When I retrieve the terminal plan from the persistence database
    Then the terminal plan processing_state should be "errored"

  Scenario: Plan from action records cancelled terminal state
    Given a persisted action "local/cancel-action" in available state
    And a plan from action "local/cancel-action" in apply phase with state "cancelled"
    When I retrieve the terminal plan from the persistence database
    Then the terminal plan processing_state should be "cancelled"
