Feature: ACMS context list and context add CLI commands
  As a CleverAgents user
  I want to list indexed context entries and add files/directories to ACMS
  So that I can manage the Advanced Context Management System (ACMS) index

  Background:
    Given a temporary project for ACMS context testing
    And the ACMS context service is initialized

  # ── context list ──────────────────────────────────────────

  Scenario: List context with no entries
    When I run context list
    Then the context list command should succeed
    And the output should indicate no files in context

  Scenario: List context with entries displays table
    Given context entries exist with:
      | path | tier | size | last_accessed |
      | src/main.py | hot | 1024 | 2026-04-15T10:00:00Z |
      | src/utils.py | warm | 2048 | 2026-04-14T10:00:00Z |
    When I run context list
    Then the context list command should succeed
    And the output should display a table with columns: File Path, Type, Size, Added
    And the table should contain "main.py"
    And the table should contain "utils.py"

  Scenario: List context with JSON format
    Given context entries exist with:
      | path | tier | size | last_accessed |
      | src/main.py | hot | 1024 | 2026-04-15T10:00:00Z |
    When I run context list with format "json"
    Then the context list command should succeed
    And the output should be valid JSON
    And the JSON output should contain an array of context entries
    And each entry should have keys: path, tier, size, last_accessed

  Scenario: List context with table format (explicit)
    Given context entries exist with:
      | path | tier | size | last_accessed |
      | src/main.py | hot | 1024 | 2026-04-15T10:00:00Z |
    When I run context list with format "table"
    Then the context list command should succeed
    And the output should display a table

  Scenario: List context shows tier information
    Given context entries exist with:
      | path | tier | size | last_accessed |
      | hot_file.py | hot | 512 | 2026-04-15T10:00:00Z |
      | warm_file.py | warm | 1024 | 2026-04-14T10:00:00Z |
      | cold_file.py | cold | 2048 | 2026-04-13T10:00:00Z |
    When I run context list
    Then the context list command should succeed
    And the output should contain "hot"
    And the output should contain "warm"
    And the output should contain "cold"

  Scenario: List context shows file sizes
    Given context entries exist with:
      | path | tier | size | last_accessed |
      | small.py | hot | 512 | 2026-04-15T10:00:00Z |
      | large.py | warm | 10240 | 2026-04-14T10:00:00Z |
    When I run context list
    Then the context list command should succeed
    And the output should contain "512 bytes"
    And the output should contain "10,240 bytes"

  Scenario: List context shows last-accessed metadata
    Given context entries exist with:
      | path | tier | size | last_accessed |
      | recent.py | hot | 1024 | 2026-04-15T10:00:00Z |
      | old.py | cold | 2048 | 2026-04-01T10:00:00Z |
    When I run context list
    Then the context list command should succeed
    And the output should contain "2026-04-15"
    And the output should contain "2026-04-01"

  Scenario: List context with --help shows documentation
    When I run context list with --help
    Then the output should contain "List all files in the current plan's context"
    And the output should contain "--format"

  # ── context add ───────────────────────────────────────────

  Scenario: Add single file to context
    Given a file "src/main.py" exists
    When I run context add "src/main.py"
    Then the context add command should succeed
    And the output should indicate file was added
    And the context should contain "src/main.py"

  Scenario: Add directory to context recursively
    Given a directory "src" exists with files:
      | file |
      | main.py |
      | utils.py |
      | helpers.py |
    When I run context add "src" with --recursive
    Then the context add command should succeed
    And the output should indicate 3 files were added
    And the context should contain "src/main.py"
    And the context should contain "src/utils.py"
    And the context should contain "src/helpers.py"

  Scenario: Add directory without recursive flag
    Given a directory "src" exists with files:
      | file |
      | main.py |
    When I run context add "src" without --recursive
    Then the context add command should succeed
    And the context should contain "src"

  Scenario: Add file with --tag flag
    Given a file "src/main.py" exists
    When I run context add "src/main.py" with --tag "production"
    Then the context add command should succeed
    And the context entry for "src/main.py" should have tag "production"

  Scenario: Add file with --policy flag
    Given a file "src/main.py" exists
    When I run context add "src/main.py" with --policy "hot"
    Then the context add command should succeed
    And the context entry for "src/main.py" should have policy "hot"

  Scenario: Add file with both --tag and --policy flags
    Given a file "src/main.py" exists
    When I run context add "src/main.py" with --tag "core" and --policy "warm"
    Then the context add command should succeed
    And the context entry for "src/main.py" should have tag "core"
    And the context entry for "src/main.py" should have policy "warm"

  Scenario: Add file that already exists in context
    Given a file "src/main.py" exists
    And "src/main.py" is already in context
    When I run context add "src/main.py"
    Then the context add command should succeed
    And the output should indicate file was already in context

  Scenario: Add non-existent file fails
    When I run context add "nonexistent.py"
    Then the context add command should fail
    And the output should contain "Path does not exist"

  Scenario: Add multiple files at once
    Given files exist:
      | file |
      | src/main.py |
      | src/utils.py |
      | tests/test_main.py |
    When I run context add "src/main.py" "src/utils.py" "tests/test_main.py"
    Then the context add command should succeed
    And the output should indicate 3 files were added
    And the context should contain "src/main.py"
    And the context should contain "src/utils.py"
    And the context should contain "tests/test_main.py"

  Scenario: Add with --help shows documentation
    When I run context add with --help
    Then the output should contain "Add files or directories to the current plan's context"
    And the output should contain "--tag"
    And the output should contain "--policy"
    And the output should contain "--recursive"

  Scenario: Add with JSON output format
    Given a file "src/main.py" exists
    When I run context add "src/main.py" with format "json"
    Then the context add command should succeed
    And the output should be valid JSON
    And the JSON output should contain key "added_files"
    And the JSON output should contain key "status"

  # ── context list and add integration ───────────────────────

  Scenario: Add file then list shows it
    Given a file "src/main.py" exists
    When I run context add "src/main.py"
    And I run context list
    Then the context list command should succeed
    And the output should contain "main.py"

  Scenario: Add multiple files with different policies then list
    Given files exist:
      | file |
      | src/main.py |
      | src/utils.py |
    When I run context add "src/main.py" with --policy "hot"
    And I run context add "src/utils.py" with --policy "warm"
    And I run context list
    Then the context list command should succeed
    And the output should contain "main.py"
    And the output should contain "utils.py"
    And the output should contain "hot"
    And the output should contain "warm"

  Scenario: List JSON format includes all metadata
    Given a file "src/main.py" exists with --tag "core" and --policy "hot"
    When I run context add "src/main.py" with --tag "core" and --policy "hot"
    And I run context list with format "json"
    Then the context list command should succeed
    And the JSON output should contain entries with:
      | field | value |
      | path | src/main.py |
      | tag | core |
      | policy | hot |
