Feature: Project Service
  As a developer
  I want to manage projects through the project service
  So that I can organize my CleverAgents work

  Scenario: Initialize a new project
    Given I have a project service
    When I initialize a project "test-project" at "/tmp/test"
    Then the project should be created successfully
    And the project name should be "test-project"
    And the project path should be "/tmp/test"
    And the .cleveragents directory should exist at "/tmp/test/.cleveragents"

  Scenario: Cannot initialize project twice without force
    Given I have a project service
    And I have initialized a service project "first" at "/tmp/test"
    When I try to initialize a project "second" at "/tmp/test" without force
    Then I should get a ValidationError
    And the error message should contain "already initialized"

  Scenario: Force reinitialize a project
    Given I have a project service
    And I have initialized a service project "first" at "/tmp/test"
    When I initialize a project "second" at "/tmp/test" with force
    Then the project should be created successfully
    And the project name should be "second"

  Scenario: Get current project from working directory
    Given I have a project service
    And I am in a project directory
    When I get the current project
    Then I should receive the project information
    And the project should have a name
    And the project should have a path

  Scenario: Get project statistics
    Given I have a project service
    And I have a current project
    When I get project statistics
    Then I should receive statistics
    And the statistics should include "plans"
    And the statistics should include "context_files"
    And the statistics should include "changes"

  Scenario: Initialization fails gracefully when filesystem operations error
    Given I have a project service
    And I prepare a project path "fs-error-project"
    When I initialize the project with filesystem failure
    Then a FileSystemError should be raised with message "Failed to create project structure"

  Scenario: Duplicate project name uses database validation when directory missing
    Given I have a project service
    And I prepare a project path "duplicate-db"
    And I have initialized that project once already
    And the cleveragents directory has been removed for that project
    When I initialize the same project without force using database state
    Then I should get a ValidationError
    And the error message should contain "already exists"

  Scenario: Migration returns existing project without creating duplicates
    Given I have a project service
    And I prepare a project path "migration-existing"
    And I have initialized that project once already
    And the cleveragents directory has been removed for that project
    And the legacy migration will report success without changes
    When I initialize the same project without force during migration
    Then the existing project should be reused

  Scenario: Fallback current project is synthesized when database lacks record
    Given I have a project service
    And I set up a standalone project directory named "standalone-fallback" with a name file
    When I fetch the current project from that directory without database entry
    Then a temporary project named "standalone-fallback" should be returned

  Scenario: Legacy project without name file uses directory name
    Given I have a project service
    And I set up a standalone project directory named "legacy-no-name" without a name file
    When I fetch the current project from that directory without database entry
    Then a temporary project named "legacy-no-name" should be returned

  Scenario: No project directory results in no current project
    Given I have a project service
    And I prepare an empty working directory
    When I fetch the current project from that directory without database entry
    Then no current project should be found

  Scenario: Retrieve project by name returns saved project
    Given I have a project service
    And I have initialized a service project "lookup-project" at "/tmp/test"
    When I try to get a project by name "lookup-project"
    Then the project lookup result should be found

  Scenario: Create project alias delegates to initializer
    Given I have a project service
    And I prepare a project path "alias-project"
    When I create the project using the alias method
    Then the alias project should be created successfully

  Scenario: Retrieve project by path finds the matching record
    Given I have a project service
    And I have initialized a service project "path-project" at "/tmp/test"
    When I look up the project by its saved path
    Then the project lookup result should be found

  Scenario: Default ordering falls back to created_at
    Given I have a project service
    And I have multiple projects created at different times
    When I list all projects with unknown ordering
    Then the projects should be returned in creation order
