@phase1 @domain @repository @plan_repository
Feature: Lifecycle Plan Repository Persistence
  As a system operator managing lifecycle plans
  I want the plan repository to reliably persist and retrieve plans
  So that plan state survives process restarts and supports CLI list/filter commands

  Background:
    Given a clean in-memory database for plan repository tests
    And a lifecycle plan repository backed by the database

  # ---------------------------------------------------------------------------
  # Create and retrieve
  # ---------------------------------------------------------------------------

  @plan_create @plan_get
  Scenario: Create a plan and retrieve it by ID
    Given a valid plan domain object with id "01HGZ6FE0AQDYTR4BX00000001"
    When the plan is saved through the plan repository
    Then the plan repository should not raise an error
    And the plan retrieved by id "01HGZ6FE0AQDYTR4BX00000001" should exist
    And the retrieved plan description should be "Test plan description"

  @plan_create @plan_get_by_name
  Scenario: Create a plan and retrieve it by name
    Given a valid plan domain object with id "01HGZ6FE0AQDYTR4BX00000002"
    When the plan is saved through the plan repository
    Then the plan retrieved by name "local/test-plan" should exist

  # ---------------------------------------------------------------------------
  # Update
  # ---------------------------------------------------------------------------

  @plan_update
  Scenario: Update plan phase and processing state
    Given a valid plan domain object with id "01HGZ6FE0AQDYTR4BX00000003"
    And the plan is saved through the plan repository
    When the plan phase is updated to "strategize" with state "processing"
    Then the retrieved plan should have phase "strategize"
    And the retrieved plan should have processing state "processing"

  # ---------------------------------------------------------------------------
  # List filters
  # ---------------------------------------------------------------------------

  @plan_list @plan_filter_phase
  Scenario: List plans filtered by phase
    Given 3 plans exist in phase "action" and 2 plans in phase "strategize"
    When plans are listed with phase filter "action"
    Then the plan list should contain 3 plans

  @plan_list @plan_filter_state
  Scenario: List plans filtered by processing state
    Given 2 plans exist in state "queued" and 1 plan in state "processing"
    When plans are listed with processing state filter "queued"
    Then the plan list should contain 2 plans

  @plan_list @plan_filter_action
  Scenario: List plans filtered by action name
    Given 2 plans referencing action "local/deploy" and 1 plan referencing "local/review"
    When plans are listed with action name filter "local/deploy"
    Then the plan list should contain 2 plans

  @plan_list @plan_filter_project
  Scenario: List plans filtered by project name
    Given a plan linked to project "local/api-service" and another with no project link
    When plans are listed with project name filter "local/api-service"
    Then the plan list should contain 1 plan

  # ---------------------------------------------------------------------------
  # Delete
  # ---------------------------------------------------------------------------

  @plan_delete
  Scenario: Delete plan cascades to child tables
    Given a valid plan domain object with id "01HGZ6FE0AQDYTR4BX00000010" and child rows
    And the plan is saved through the plan repository
    When the plan with id "01HGZ6FE0AQDYTR4BX00000010" is deleted
    Then the plan retrieved by id "01HGZ6FE0AQDYTR4BX00000010" should not exist
    And the child rows for plan "01HGZ6FE0AQDYTR4BX00000010" should be gone

  # ---------------------------------------------------------------------------
  # Error handling
  # ---------------------------------------------------------------------------

  @plan_create @error_handling
  Scenario: Creating duplicate plan raises error
    Given a valid plan domain object with id "01HGZ6FE0AQDYTR4BX00000011"
    And the plan is saved through the plan repository
    When a duplicate plan with id "01HGZ6FE0AQDYTR4BX00000011" is saved
    Then a DuplicatePlanError should be raised mentioning "01HGZ6FE0AQDYTR4BX00000011"

  @plan_get @error_handling
  Scenario: Getting non-existent plan returns None
    When the plan is retrieved by id "01HGZ6FE0AQDYTR4BXZZZZZZZZ"
    Then the plan retrieve result should be None

  # ---------------------------------------------------------------------------
  # Phase and state persistence
  # ---------------------------------------------------------------------------

  @plan_phase @action_phase
  Scenario: Plan with Action phase persists correctly
    Given a plan domain object in phase "action" with state "queued" and id "01HGZ6FE0AQDYTR4BX00000012"
    When the plan is saved through the plan repository
    Then the retrieved plan should have phase "action"
    And the retrieved plan should have processing state "queued"

  @plan_phase @apply_terminal
  Scenario: Plan with Apply terminal state persists correctly
    Given a plan domain object in phase "apply" with state "applied" and id "01HGZ6FE0AQDYTR4BX00000013"
    When the plan is saved through the plan repository
    Then the retrieved plan should have phase "apply"
    And the retrieved plan should have processing state "applied"

  # ---------------------------------------------------------------------------
  # Child row ordering
  # ---------------------------------------------------------------------------

  @plan_children @ordering
  Scenario: Child rows (projects, arguments, invariants) persist in order
    Given a plan with ordered children and id "01HGZ6FE0AQDYTR4BX00000014"
    When the plan is saved through the plan repository
    Then the retrieved plan should have arguments in order "alpha,beta,gamma"
    And the retrieved plan should have invariants in order "first,second,third"
    And the retrieved plan should have project links including "local/proj-a"
