Feature: Plan lifecycle model validation
  Verify that the plan lifecycle model enforces the specification
  constraints: ERRORED terminality, per-phase state validation,
  and rejection of invalid phase/state combinations.

  # ---------------------------------------------------------------
  # ERRORED is terminal (spec alignment)
  # ---------------------------------------------------------------

  Scenario: ERRORED state is terminal
    Given a plmv plan in STRATEGIZE phase with ERRORED state
    Then the plmv plan should be terminal

  # ---------------------------------------------------------------
  # APPLIED only valid in APPLY phase
  # ---------------------------------------------------------------

  Scenario: APPLIED only valid in APPLY phase
    When I try to create a plmv plan with phase STRATEGIZE and state APPLIED
    Then a plmv validation error should be raised containing "APPLIED is only valid in the APPLY phase"

  # ---------------------------------------------------------------
  # CONSTRAINED only valid in APPLY phase
  # ---------------------------------------------------------------

  Scenario: CONSTRAINED only valid in APPLY phase
    When I try to create a plmv plan with phase EXECUTE and state CONSTRAINED
    Then a plmv validation error should be raised containing "CONSTRAINED is only valid in the APPLY phase"

  # ---------------------------------------------------------------
  # COMPLETE only valid in STRATEGIZE or EXECUTE phases
  # ---------------------------------------------------------------

  Scenario: COMPLETE only valid in STRATEGIZE or EXECUTE phase
    When I try to create a plmv plan with phase APPLY and state COMPLETE
    Then a plmv validation error should be raised containing "COMPLETE is only valid in STRATEGIZE or EXECUTE"

  # ---------------------------------------------------------------
  # QUEUED valid in any phase
  # ---------------------------------------------------------------

  Scenario: QUEUED valid in any phase
    Given a plmv plan in STRATEGIZE phase with QUEUED state
    And a plmv plan in EXECUTE phase with QUEUED state
    And a plmv plan in APPLY phase with QUEUED state
    Then all plmv plans should be valid

  # ---------------------------------------------------------------
  # Additional coverage: valid combos
  # ---------------------------------------------------------------

  Scenario: APPLIED is valid in APPLY phase
    Given a plmv plan in APPLY phase with APPLIED state
    Then the plmv plan should be terminal

  Scenario: CONSTRAINED is valid in APPLY phase
    Given a plmv plan in APPLY phase with CONSTRAINED state
    Then the plmv plan should be terminal

  Scenario: COMPLETE is valid in STRATEGIZE phase
    Given a plmv plan in STRATEGIZE phase with COMPLETE state
    Then the plmv plan should not be terminal

  Scenario: COMPLETE is valid in EXECUTE phase
    Given a plmv plan in EXECUTE phase with COMPLETE state
    Then the plmv plan should not be terminal

  Scenario: ERRORED in APPLY phase is also terminal
    Given a plmv plan in APPLY phase with ERRORED state
    Then the plmv plan should be terminal

  Scenario: CANCELLED in EXECUTE phase is terminal
    Given a plmv plan in EXECUTE phase with CANCELLED state
    Then the plmv plan should be terminal

  # ---------------------------------------------------------------
  # Invalid combos for ACTION phase
  # ---------------------------------------------------------------

  Scenario: COMPLETE is not valid in ACTION phase
    When I try to create a plmv plan with phase ACTION and state COMPLETE
    Then a plmv validation error should be raised containing "COMPLETE is only valid in STRATEGIZE or EXECUTE"

  Scenario: APPLIED is not valid in EXECUTE phase
    When I try to create a plmv plan with phase EXECUTE and state APPLIED
    Then a plmv validation error should be raised containing "APPLIED is only valid in the APPLY phase"

  Scenario: APPLIED is not valid in ACTION phase
    When I try to create a plmv plan with phase ACTION and state APPLIED
    Then a plmv validation error should be raised containing "APPLIED is only valid in the APPLY phase"

  Scenario: CONSTRAINED is not valid in ACTION phase
    When I try to create a plmv plan with phase ACTION and state CONSTRAINED
    Then a plmv validation error should be raised containing "CONSTRAINED is only valid in the APPLY phase"

  Scenario: CONSTRAINED is not valid in STRATEGIZE phase
    When I try to create a plmv plan with phase STRATEGIZE and state CONSTRAINED
    Then a plmv validation error should be raised containing "CONSTRAINED is only valid in the APPLY phase"

  # ---------------------------------------------------------------
  # PROCESSING valid in any phase (T1)
  # ---------------------------------------------------------------

  Scenario: PROCESSING valid in any phase
    Given a plmv plan in ACTION phase with PROCESSING state
    And a plmv plan in STRATEGIZE phase with PROCESSING state
    And a plmv plan in EXECUTE phase with PROCESSING state
    And a plmv plan in APPLY phase with PROCESSING state
    Then all plmv plans should be valid

  # ---------------------------------------------------------------
  # ERRORED valid in all phases including ACTION (T2)
  # ---------------------------------------------------------------

  Scenario: ERRORED in ACTION phase is terminal
    Given a plmv plan in ACTION phase with ERRORED state
    Then the plmv plan should be terminal

  Scenario: ERRORED in EXECUTE phase is terminal
    Given a plmv plan in EXECUTE phase with ERRORED state
    Then the plmv plan should be terminal

  # ---------------------------------------------------------------
  # CANCELLED valid in all phases (additional)
  # ---------------------------------------------------------------

  Scenario: CANCELLED in ACTION phase is terminal
    Given a plmv plan in ACTION phase with CANCELLED state
    Then the plmv plan should be terminal

  Scenario: CANCELLED in STRATEGIZE phase is terminal
    Given a plmv plan in STRATEGIZE phase with CANCELLED state
    Then the plmv plan should be terminal

  Scenario: CANCELLED in APPLY phase is terminal
    Given a plmv plan in APPLY phase with CANCELLED state
    Then the plmv plan should be terminal

  # ---------------------------------------------------------------
  # Assignment-order regression: state-before-phase (T6)
  # ---------------------------------------------------------------

  Scenario: Phase transition with state-first assignment does not raise
    Given a plmv plan in STRATEGIZE phase with COMPLETE state
    When I plmv transition the plan to EXECUTE phase with QUEUED state using state-first order
    Then the plmv plan should have phase EXECUTE and state QUEUED

  # ---------------------------------------------------------------
  # Assignment-order negative: phase-first with invalid intermediate
  # ---------------------------------------------------------------

  Scenario: Phase-first assignment with invalid intermediate state raises
    Given a plmv plan in STRATEGIZE phase with COMPLETE state
    When I plmv transition the plan to APPLY phase with APPLIED state using phase-first order
    Then a plmv validation error should be raised containing "COMPLETE is only valid in STRATEGIZE or EXECUTE"

  # ---------------------------------------------------------------
  # ERRORED terminality blocks cancel, pause, resume (lifecycle)
  # ---------------------------------------------------------------

  Scenario: ERRORED plan cannot be cancelled because it is terminal
    Given a plmv plan in EXECUTE phase with ERRORED state
    Then the plmv plan should be terminal
    And the plmv plan should not be cancellable

  Scenario: ERRORED plan cannot be paused because it is terminal
    Given a plmv plan in STRATEGIZE phase with ERRORED state
    Then the plmv plan should be terminal
    And the plmv plan should not be pausable

  Scenario: ERRORED plan cannot be resumed via lifecycle because it is terminal
    Given a plmv plan in EXECUTE phase with ERRORED state
    Then the plmv plan should be terminal
    And the plmv plan should not be resumable via lifecycle

  # ---------------------------------------------------------------
  # Deserialization coercion for legacy DB rows
  # ---------------------------------------------------------------

  Scenario: Legacy APPLY/COMPLETE is coerced to APPLY/APPLIED on deserialization
    When I plmv deserialize a plan with phase "apply" and state "complete"
    Then the plmv deserialized plan should have phase APPLY and state APPLIED

  Scenario: Legacy STRATEGIZE/APPLIED is coerced to STRATEGIZE/ERRORED on deserialization
    When I plmv deserialize a plan with phase "strategize" and state "applied"
    Then the plmv deserialized plan should have phase STRATEGIZE and state ERRORED

  Scenario: Legacy ACTION/COMPLETE is coerced to ACTION/QUEUED on deserialization
    When I plmv deserialize a plan with phase "action" and state "complete"
    Then the plmv deserialized plan should have phase ACTION and state QUEUED
