Feature: Enriched PLAN_CANCELLED event payload
  As an audit log consumer
  I want the PLAN_CANCELLED domain event to include progress and resource cleanup context
  So that I can understand what state the plan was in when it was cancelled
  and what resources need cleanup

  Background:
    Given I have a plan lifecycle service for cancelled enrichment tests

  # ---------------------------------------------------------------
  # Core enrichment: progress context fields
  # ---------------------------------------------------------------

  Scenario: PLAN_CANCELLED event includes phase at cancellation
    Given an action "local/enrich-cancel-phase" exists for enrichment tests
    And a plan created from "local/enrich-cancel-phase" for enrichment tests
    When I cancel the plan for enrichment tests with reason "user requested"
    Then the PLAN_CANCELLED event details should include "cancelled_phase"

  Scenario: PLAN_CANCELLED event includes processing state at cancellation
    Given an action "local/enrich-cancel-state" exists for enrichment tests
    And a plan created from "local/enrich-cancel-state" for enrichment tests
    When I cancel the plan for enrichment tests with reason "timeout"
    Then the PLAN_CANCELLED event details should include "cancelled_processing_state"

  Scenario: PLAN_CANCELLED event includes last completed step
    Given an action "local/enrich-cancel-step" exists for enrichment tests
    And a plan created from "local/enrich-cancel-step" for enrichment tests
    When I cancel the plan for enrichment tests with reason "budget exceeded"
    Then the PLAN_CANCELLED event details should include "last_completed_step"

  Scenario: PLAN_CANCELLED event includes subplan count
    Given an action "local/enrich-cancel-subplan" exists for enrichment tests
    And a plan created from "local/enrich-cancel-subplan" for enrichment tests
    When I cancel the plan for enrichment tests with reason "manual"
    Then the PLAN_CANCELLED event details should include "subplan_count"

  # ---------------------------------------------------------------
  # Core enrichment: resource cleanup context fields
  # ---------------------------------------------------------------

  Scenario: PLAN_CANCELLED event includes sandbox refs for cleanup
    Given an action "local/enrich-cancel-sandbox" exists for enrichment tests
    And a plan created from "local/enrich-cancel-sandbox" for enrichment tests
    When I cancel the plan for enrichment tests with reason "resource limit"
    Then the PLAN_CANCELLED event details should include "sandbox_refs"

  Scenario: PLAN_CANCELLED event includes changeset id for cleanup
    Given an action "local/enrich-cancel-changeset" exists for enrichment tests
    And a plan created from "local/enrich-cancel-changeset" for enrichment tests
    When I cancel the plan for enrichment tests with reason "rollback"
    Then the PLAN_CANCELLED event details should include "changeset_id"

  Scenario: PLAN_CANCELLED event includes resources pending cleanup count
    Given an action "local/enrich-cancel-resources" exists for enrichment tests
    And a plan created from "local/enrich-cancel-resources" for enrichment tests
    When I cancel the plan for enrichment tests with reason "cleanup test"
    Then the PLAN_CANCELLED event details should include "resources_pending_cleanup"

  # ---------------------------------------------------------------
  # Cancellation reason field
  # ---------------------------------------------------------------

  Scenario: PLAN_CANCELLED event includes cancellation reason
    Given an action "local/enrich-cancel-reason" exists for enrichment tests
    And a plan created from "local/enrich-cancel-reason" for enrichment tests
    When I cancel the plan for enrichment tests with reason "explicit reason"
    Then the PLAN_CANCELLED event details should have reason "explicit reason"

  Scenario: PLAN_CANCELLED event reason defaults to empty string when not provided
    Given an action "local/enrich-cancel-no-reason" exists for enrichment tests
    And a plan created from "local/enrich-cancel-no-reason" for enrichment tests
    When I cancel the plan for enrichment tests without a reason
    Then the PLAN_CANCELLED event reason field should be empty string

  # ---------------------------------------------------------------
  # Progress context accuracy
  # ---------------------------------------------------------------

  Scenario: PLAN_CANCELLED event captures phase accurately as Strategize
    Given an action "local/enrich-cancel-strategize" exists for enrichment tests
    And a plan created from "local/enrich-cancel-strategize" for enrichment tests
    When I cancel the plan for enrichment tests with reason "cancelled in strategize"
    Then the PLAN_CANCELLED event details should have cancelled_phase "strategize"

  Scenario: PLAN_CANCELLED event captures last_completed_step value accurately
    Given an action "local/enrich-cancel-step-val" exists for enrichment tests
    And a plan created from "local/enrich-cancel-step-val" for enrichment tests
    When I cancel the plan for enrichment tests with reason "step check"
    Then the PLAN_CANCELLED event details should have last_completed_step -1

  # ---------------------------------------------------------------
  # Resource cleanup context accuracy
  # ---------------------------------------------------------------

  Scenario: PLAN_CANCELLED event captures sandbox refs accurately when present
    Given an action "local/enrich-cancel-sandbox-val" exists for enrichment tests
    And a plan with sandbox refs created from "local/enrich-cancel-sandbox-val"
    When I cancel the plan for enrichment tests with reason "sandbox cleanup"
    Then the PLAN_CANCELLED event details should have sandbox_refs count 2

  Scenario: PLAN_CANCELLED event captures resources_pending_cleanup count accurately
    Given an action "local/enrich-cancel-pending" exists for enrichment tests
    And a plan with sandbox refs created from "local/enrich-cancel-pending"
    When I cancel the plan for enrichment tests with reason "pending cleanup"
    Then the PLAN_CANCELLED event details should have resources_pending_cleanup 2

  # ---------------------------------------------------------------
  # Backward compatibility: existing fields still present
  # ---------------------------------------------------------------

  Scenario: PLAN_CANCELLED event still includes project_names field
    Given an action "local/enrich-cancel-compat" exists for enrichment tests
    And a plan created from "local/enrich-cancel-compat" for enrichment tests
    When I cancel the plan for enrichment tests with reason "compat check"
    Then the PLAN_CANCELLED event details should include "project_names"

  Scenario: PLAN_CANCELLED event still includes reason field
    Given an action "local/enrich-cancel-compat-reason" exists for enrichment tests
    And a plan created from "local/enrich-cancel-compat-reason" for enrichment tests
    When I cancel the plan for enrichment tests with reason "compat reason"
    Then the PLAN_CANCELLED event details should include "reason"
