Feature: Plan use --execution-env-priority flag
  As a developer
  I want to set execution environment priority when using plan use
  So that I can control whether the environment setting is a fallback or override

  Background:
    Given a plan env priority CLI runner
    And a plan env priority mocked lifecycle service
    And a plan env priority action exists

  Scenario: Plan use with --execution-env-priority override and --execution-environment
    When I run plan use with execution environment "container" and priority "override"
    Then the plan env priority command should succeed
    And the plan should have execution_env_priority set to "override"
    And the plan should have execution_environment set to "container"
    And the plan env priority service use_action should have been called
    And the plan env priority service save_plan should have been called

  Scenario: Plan use with --execution-env-priority fallback and --execution-environment
    When I run plan use with execution environment "host" and priority "fallback"
    Then the plan env priority command should succeed
    And the plan should have execution_env_priority set to "fallback"
    And the plan should have execution_environment set to "host"

  Scenario: Plan use with --execution-env-priority without --execution-environment fails
    When I run plan use with priority "override" but no execution environment
    Then the plan env priority command should fail
    And the plan env priority output should contain "--execution-env-priority requires --execution-environment"

  Scenario: Plan use with --execution-environment but no --execution-env-priority defaults to fallback
    When I run plan use with execution environment "container" but no priority
    Then the plan env priority command should succeed
    And the plan should have execution_env_priority set to "fallback"
    And the plan env priority service save_plan should have been called

  Scenario: Plan use with invalid --execution-env-priority value fails
    When I run plan use with execution environment "container" and priority "invalid-value"
    Then the plan env priority command should fail
    And the plan env priority output should contain "Invalid execution env priority"

  Scenario: Plan use output displays priority when execution environment is set
    When I run plan use with execution environment "container" and priority "override"
    Then the plan env priority command should succeed
    And the plan env priority output should contain "Execution Env Priority"
    And the plan env priority output should contain "override"

  Scenario: Plan use JSON output includes execution_env_priority
    When I run plan use with execution environment "container" and priority "override" in json format
    Then the plan env priority command should succeed
    And the plan env priority json output should include "execution_env_priority" as "override"

  Scenario: Plan use accepts uppercase --execution-env-priority value
    When I run plan use with execution environment "container" and priority "OVERRIDE"
    Then the plan env priority command should succeed
    And the plan should have execution_env_priority set to "override"

  # --- Domain model validator tests ---

  Scenario: Plan construction with execution_env_priority but no execution_environment raises ValueError
    When I construct a Plan with execution_env_priority "override" and no execution_environment
    Then the plan construction should raise ValueError
    And the plan construction error should mention "execution_env_priority requires execution_environment"

  Scenario: Plan construction with both execution_environment and execution_env_priority succeeds
    When I construct a Plan with execution_environment "container" and execution_env_priority "override"
    Then the plan construction should succeed
    And the constructed plan should have execution_env_priority "override"
    And the constructed plan should have execution_environment "container"

  # --- ExecutionEnvPriority enum tests ---

  Scenario: ExecutionEnvPriority enum has fallback value
    Then the ExecutionEnvPriority enum should have value "fallback"

  Scenario: ExecutionEnvPriority enum has override value
    Then the ExecutionEnvPriority enum should have value "override"

  Scenario: ExecutionEnvPriority is a StrEnum subclass
    Then ExecutionEnvPriority should be a StrEnum subclass

  # --- Plan.as_cli_dict execution environment tests ---

  Scenario: as_cli_dict includes both execution fields when set
    Given a plan with execution_environment "container" and execution_env_priority "override"
    When I call as_cli_dict on the env priority plan
    Then the env priority CLI dict should contain key "execution_environment" with value "container"
    And the env priority CLI dict should contain key "execution_env_priority" with value "override"

  Scenario: as_cli_dict omits execution fields when execution_environment is None
    Given a plan with no execution_environment
    When I call as_cli_dict on the env priority plan
    Then the env priority CLI dict should not contain key "execution_environment"
    And the env priority CLI dict should not contain key "execution_env_priority"

  Scenario: as_cli_dict defaults execution_env_priority to fallback for pre-migration data
    Given a plan with execution_environment "host" but no execution_env_priority
    When I call as_cli_dict on the env priority plan
    Then the env priority CLI dict should contain key "execution_environment" with value "host"
    And the env priority CLI dict should contain key "execution_env_priority" with value "fallback"

  # --- DB round-trip serialization tests ---

  Scenario: LifecyclePlanModel round-trip preserves execution environment fields
    Given a plan with execution_environment "container" and execution_env_priority "override"
    When I serialize the plan through LifecyclePlanModel from_domain and to_domain
    Then the round-trip plan should have execution_environment "container"
    And the round-trip plan should have execution_env_priority "override"

  Scenario: LifecyclePlanModel round-trip preserves None execution environment fields
    Given a plan with no execution_environment
    When I serialize the plan through LifecyclePlanModel from_domain and to_domain
    Then the round-trip plan should have execution_environment None
    And the round-trip plan should have execution_env_priority None
