Feature: Definition-of-Done Gating in Apply Phase
  As a developer
  I want the Apply phase to evaluate Definition-of-Done criteria
  So that plans cannot be applied until all required DoD criteria pass

  Background:
    Given I have a plan lifecycle service for DoD gating

  # --- DoD evaluation is skipped when no DoD text is set ---

  Scenario: Apply succeeds when plan has no definition of done
    Given I have a plan with no definition of done in execute complete state
    When I apply the plan with DoD gating
    Then the plan should transition to apply phase
    And the plan validation summary should be empty

  Scenario: Apply succeeds when definition of done is empty string
    Given I have a plan with empty definition of done in execute complete state
    When I apply the plan with DoD gating
    Then the plan should transition to apply phase
    And the plan validation summary should be empty

  # --- DoD evaluation runs and passes ---

  Scenario: Apply succeeds when all DoD criteria pass via context match
    Given I have a plan with definition of done "tests_passed" and argument "tests_passed" equal to "true"
    When I apply the plan with DoD gating
    Then the plan should transition to apply phase
    And the plan validation summary should record dod_evaluated as true
    And the plan validation summary should record dod_all_passed as true

  Scenario: Apply succeeds when DoD criteria are skipped due to empty context
    Given I have a plan with definition of done "all tests pass" and no arguments
    When I apply the plan with DoD gating
    Then the plan should transition to apply phase
    And the plan validation summary should record dod_evaluated as true

  # --- DoD evaluation blocks apply when criteria fail ---

  Scenario: Apply is blocked when DoD criteria fail
    Given I have a plan with definition of done "coverage_above_97" and argument "status" equal to "incomplete"
    When I try to apply the plan with DoD gating
    Then a DoD gating error should be raised
    And the plan should remain in execute phase
    And the plan validation summary should record dod_evaluated as true
    And the plan validation summary should record dod_all_passed as false

  Scenario: DoDGatingError contains correct failure counts
    Given I have a plan with definition of done "criterion_alpha" and argument "status" equal to "incomplete"
    When I try to apply the plan with DoD gating
    Then a DoD gating error should be raised
    And the DoD gating error should report 1 failed criteria out of 1 total

  # --- DoD evaluation with template rendering ---

  Scenario: DoD template placeholders are rendered with plan arguments
    Given I have a plan with definition of done "{status}" and argument "status" equal to "complete"
    When I apply the plan with DoD gating
    Then the plan should transition to apply phase

  # --- validation_summary is populated ---

  Scenario: validation_summary is populated after DoD evaluation
    Given I have a plan with definition of done "tests_passed" and argument "tests_passed" equal to "true"
    When I apply the plan with DoD gating
    Then the plan validation summary should contain key "dod_evaluated" with value true
    And the plan validation summary should contain key "dod_all_passed" with value true
    And the plan validation summary should contain key "dod"

  # --- DoD gating integrates with full lifecycle ---

  Scenario: Plan with passing DoD can complete full apply lifecycle
    Given I have a plan with definition of done "tests_passed" and argument "tests_passed" equal to "true"
    When I apply the plan with DoD gating
    And I start the apply phase
    And I complete the apply phase
    Then the dod plan processing state should be "applied"

  Scenario: Plan with failing DoD stays in execute phase after blocked apply
    Given I have a plan with definition of done "coverage_above_97" and argument "status" equal to "incomplete"
    When I try to apply the plan with DoD gating
    Then a DoD gating error should be raised
    And the dod plan phase should be "execute"
    And the dod plan processing state should be "complete"
