Feature: Database Repository Error Handling Coverage
  As a developer
  I want to ensure database repositories handle edge cases properly
  So that missing entities and failed updates are handled gracefully

  @phase1
  Scenario: ProjectRepository get_by_id returns None for non-existent project
    Given I have a project repository with database session
    When I query for a project with non-existent ID 99999
    Then the repository should return None for missing project

  @phase1
  Scenario: ProjectRepository update method handles successful project updates
    Given I have a project repository with database session
    And I have created a project named "test-project"
    When I modify the project name to "updated-project"
    Then the project should be updated in the database
    And the updated_at timestamp should be refreshed

  @phase1
  Scenario: ProjectRepository update returns unchanged object for non-existent project
    Given I have a project repository with database session
    When I attempt to update a project that does not exist
    Then the update operation should complete without error
    And the original project object should be returned unchanged

  @phase1
  Scenario: PlanRepository get_current_for_project returns None when no current plan exists
    Given I have a plan repository with database session
    When I query for current plan of non-existent project 99999
    Then the repository should return None for missing current plan

  @phase1
  Scenario: PlanRepository get_by_id returns plan with None build when no build exists
    Given I have a plan repository with database session
    And I have created a plan without build information
    When I retrieve the plan by ID
    Then the plan should be returned with null build field

  @phase1
  Scenario: ProjectRepository create raises database error on OperationalError
    Given I have a project repository with a session that fails on create
    When I attempt to create a project named "failing-project" with that failing session
    Then a database error should be raised when creating the project
    And the session rollback should be triggered for the project create failure

  @phase1
  Scenario: ProjectRepository get_by_id wraps OperationalError in DatabaseError
    Given I have a project repository with a session that fails on query
    When I query the project repository for ID 42 expecting a failure
    Then a database error should be raised for the project lookup

  @phase1
  Scenario: PlanRepository get_by_id returns None for unknown plan
    Given I have a plan repository with database session
    When I query for a plan with non-existent ID 99999
    Then the plan repository should return None for missing plan

  @phase1
  Scenario: PlanRepository update persists applied_at field when provided
    Given I have a plan repository with database session
    And I have created a plan without build information
    When I update the plan with a new applied timestamp
    Then the plan record should store the applied timestamp value

  @phase1
  Scenario: PlanRepository get_current returns None when no plan is marked current
    Given I have a plan repository with database session
    And I have created a plan without build information
    When I request the current plan for the project
    Then the repository should return None for missing current plan

  @phase1
  Scenario: ChangeRepository get_all returns persisted changes
    Given I have a plan repository with database session
    And I have created a plan without build information
    And I have a change repository with the shared database session
    And I have added a change for the plan
    When I request all changes for the plan
    Then the repository should include the added change

  @phase1
  Scenario: DebugAttemptRepository get returns None for missing attempt
    Given I have a debug attempt repository with database session
    When I query for a debug attempt with non-existent ID 404
    Then the debug attempt repository should return None for missing attempt

  @phase1
  Scenario: DebugAttemptRepository update raises error when attempt missing
    Given I have a debug attempt repository with database session
    When I attempt to update a debug attempt that does not exist
    Then the debug attempt repository should raise an error for missing attempt

  @phase1
  Scenario: DebugAttemptRepository get_all returns persisted attempts
    Given I have a debug attempt repository with database session
    And I have stored multiple debug attempts for the plan
    When I request all debug attempts
    Then the repository should return all persisted debug attempts

  @phase1
  Scenario: DebugAttemptRepository clear_for_plan removes attempts
    Given I have a debug attempt repository with database session
    And I have stored multiple debug attempts for the plan
    When I clear debug attempts for the plan
    Then the repository should return no debug attempts for that plan

  @phase1
  Scenario: ActorRepository list_all returns actors ordered by name
    Given I have an actor repository with database session
    And I have stored custom actors "provider/beta" and "provider/alpha"
    When I list actors from the repository
    Then the repository should return the actors ordered by name

  @phase1
  Scenario: ActorRepository prevents overwriting built-in actors
    Given I have an actor repository with database session
    And I have stored a built-in actor named "provider/model"
    When I try to upsert a custom actor with the same name
    Then the actor repository should raise when overwriting built-in actor

  @phase1
  Scenario: ActorRepository upsert updates existing actor values
    Given I have an actor repository with database session
    And I have stored a custom actor named "local/test-actor"
    When I update that actor with new configuration values
    Then the repository should persist the updated actor values

  @phase1
  Scenario: ActorRepository upsert_built_in marks actor as built in
    Given I have an actor repository with database session
    When I upsert a built-in actor named "provider/built-in"
    Then the actor should be stored as built-in in the repository

  @phase1
  Scenario: ActorRepository delete handles missing, built-in, default, and custom actors
    Given I have an actor repository with database session
    And I have stored a built-in actor named "provider/safe"
    And I have stored a default actor named "provider/default"
    And I have stored a custom actor named "local/deletable"
    When I delete a non-existent actor named "provider/missing"
    And I attempt to delete the built-in actor named "provider/safe"
    And I attempt to delete the default actor named "provider/default"
    And I delete the custom actor named "local/deletable"
    Then the built-in actor deletion should raise an error
    And the default actor deletion should raise an error
    And the custom actor should be removed successfully

  @phase1
  Scenario: ActorRepository set_default raises when actor is missing
    Given I have an actor repository with database session
    When I set the default actor to "provider/missing"
    Then the actor repository should raise when setting default for missing actor
