@phase1 @database @models @repositories
Feature: Database Infrastructure Models and Repositories
  As a developer
  I want to ensure database models and repositories work correctly
  So that data persistence is reliable and tested

  Background:
    Given I have imported the database infrastructure modules
    And I have a test database session

  @models @project_model
  Scenario: Create and persist a ProjectModel with all attributes
    Given I create a ProjectModel with name "test-project" and path "/test/path"
    When I set the project settings with model provider "openai" and temperature 0.7
    And I save the ProjectModel to the database
    Then the ProjectModel should be persisted with correct attributes
    And the ProjectModel should have created_at and updated_at timestamps
    And the ProjectModel settings should be correctly stored as JSON

  @models @plan_model
  Scenario: Create and persist a PlanModel with relationships
    Given I have an existing ProjectModel with id 1
    When I create a PlanModel for project 1 with name "test-plan"
    And I set the plan prompt to "Create a test feature"
    And I set the plan status to "pending"
    And I save the PlanModel to the database
    Then the PlanModel should be persisted with correct attributes
    And the PlanModel should be linked to the correct project
    And the PlanModel should have empty contexts and changes collections

  @models @context_model
  Scenario: Create and persist a ContextModel with file content
    Given I have an existing PlanModel with id 1
    When I create a ContextModel for plan 1 with type "FILE"
    And I set the context path to "/test/file.py"
    And I set the context content to "test content"
    And I set the context file_hash to "abc123"
    And I set the context size to 12
    And I save the ContextModel to the database
    Then the ContextModel should be persisted with correct attributes
    And the ContextModel should be linked to the correct plan

  @models @change_model
  Scenario: Create and persist a ChangeModel for file modification
    Given I have an existing PlanModel with id 1
    When I create a ChangeModel for plan 1 with file_path "/test/file.py"
    And I set the change operation to "MODIFY"
    And I set the original_content to "old content"
    And I set the new_content to "new content"
    And I save the ChangeModel to the database
    Then the ChangeModel should be persisted with correct attributes
    And the ChangeModel should have applied flag as False by default
    And the ChangeModel should be linked to the correct plan

  @models @database_init
  Scenario: Initialize database with all tables
    When I initialize the database with URL "sqlite:///:memory:"
    Then all database tables should be created
    And the database engine should be returned
    And I should be able to get a working session

  @repositories @project_repository
  Scenario: ProjectRepository creates and retrieves projects
    Given I have a ProjectRepository instance
    When I create a project with name "repo-test" and path "/repo/test"
    Then the project should be saved with an assigned ID
    When I retrieve the project by ID
    Then I should get the same project back
    When I retrieve the project by name "repo-test"
    Then I should get the same project back with correct attributes

  @repositories @project_repository
  Scenario: ProjectRepository updates existing projects
    Given I have a ProjectRepository instance
    And I have created a project with name "update-test"
    When I update the project name to "updated-test"
    And I update the project settings default_model to "gpt-4"
    And I save the updates
    Then the project should be updated in the database
    And the updated_at timestamp should be refreshed

  @repositories @plan_repository
  Scenario: PlanRepository creates and retrieves plans
    Given I have a PlanRepository instance
    And I ensure a project exists with id 1
    When I create a plan with name "repo-plan" for project 1
    And I update the created plan prompt to "Test prompt"
    Then the plan should be saved with an assigned ID
    When I retrieve the plan by ID
    Then I should get the same plan back with correct attributes

  @repositories @plan_repository
  Scenario: PlanRepository manages current plan for project
    Given I have a PlanRepository instance
    And I ensure a project exists with id 1
    And I have created multiple plans for the project
    When I set plan 2 as current for project 1
    Then only plan 2 should be marked as current
    When I get the current plan for project 1
    Then I should receive plan 2

  @repositories @plan_repository
  Scenario: PlanRepository retrieves all plans for a project
    Given I have a PlanRepository instance
    And I ensure a project exists with id 1
    And I have created 3 plans for the project
    When I get all plans for project 1
    Then I should receive a list of 3 plans
    And all plans should belong to project 1

  @repositories @plan_repository
  Scenario: PlanRepository updates plan with build information
    Given I have a PlanRepository instance
    And I have created a plan with id 1
    When I update the plan with build started_at timestamp
    And I update the plan with model_used "gpt-4"
    And I update the plan with token_count 1500
    And I save the plan updates
    Then the plan should have the build information persisted

  @repositories @plan_repository
  Scenario: PlanRepository updates plan with result information
    Given I have a PlanRepository instance
    And I have created a plan with id 1
    When I update the plan with applied_at timestamp
    And I update the plan with files_created 2
    And I update the plan with files_modified 3
    And I update the plan with files_deleted 1
    And I save the plan updates
    Then the plan should have the result information persisted

  @repositories @context_repository
  Scenario: ContextRepository adds and retrieves context items
    Given I have a ContextRepository instance
    And I ensure a plan exists with id 1
    When I add a context item with path "/context/file.py"
    And I set the context type to "FILE"
    And I set the context content to "file contents"
    Then the context should be saved with an assigned ID
    When I get all context items for plan 1
    Then I should receive the added context item

  @repositories @context_repository
  Scenario: ContextRepository removes specific context items
    Given I have a ContextRepository instance
    And I ensure a plan exists with id 1
    And I have added 3 context items to the plan
    When I remove context item with id 2
    Then the context item should be deleted
    When I get all context items for plan 1
    Then I should receive 2 context items
    And context item with id 2 should not be present

  @repositories @context_repository
  Scenario: ContextRepository clears all context for a plan
    Given I have a ContextRepository instance
    And I ensure a plan exists with id 1
    And I have added 5 context items to the plan
    When I clear all context for plan 1
    Then all context items should be deleted
    When I get all context items for plan 1
    Then I should receive an empty list

  @repositories @change_repository
  Scenario: ChangeRepository adds and retrieves changes
    Given I have a ChangeRepository instance
    And I ensure a plan exists with id 1
    When I add a change with file_path "/change/file.py"
    And I set the change operation to "CREATE"
    And I set the new_content to "new file content"
    Then the change should be saved with an assigned ID
    When I get all changes for plan 1
    Then I should receive the added change

  @repositories @change_repository
  Scenario: ChangeRepository marks changes as applied
    Given I have a ChangeRepository instance
    And I ensure a plan exists with id 1
    And I have added a change with id 1
    When I mark change 1 as applied
    Then the change should have applied flag set to True
    And the change should have applied_at timestamp
    When I get all changes for plan 1
    Then the retrieved change should show as applied

  @repositories @change_repository
  Scenario: ChangeRepository clears all changes for a plan
    Given I have a ChangeRepository instance
    And I ensure a plan exists with id 1
    And I have added 4 changes to the plan
    When I clear all changes for plan 1
    Then all changes should be deleted
    When I get all changes for plan 1
    Then I should receive an empty list

  @repositories @integration
  Scenario: Repository integration with domain models conversion
    Given I have all repository instances
    And I have created a project with plans, contexts, and changes
    When I retrieve the project by ID for integration
    Then I should get a domain Project model
    When I retrieve the current plan
    Then I should get a domain Plan model with build and result data
    When I retrieve contexts for the plan
    Then I should get a list of domain Context models
    When I retrieve changes for the plan
    Then I should get a list of domain Change models