Feature: Resource CLI error path coverage
  As a developer
  I want the resource CLI error handling paths thoroughly tested
  So that resource.py has high code coverage

  Background:
    Given a fresh in-memory resource registry

  # ---- type_add error paths ----

  Scenario: type add with --update flag on existing type
    Given built-in types are bootstrapped
    Given a valid custom resource type YAML file
    And I run resource type add with the config file
    When I run resource type add with the config file using update flag
    Then the resource command should fail
    And the resource output should contain "already exists"

  Scenario: type add with non-existent config triggers FileNotFoundError
    When I run resource type add with a nonexistent config file
    Then the resource command should fail
    And the resource output should contain "not found"

  Scenario: type add with validation error
    Given a custom resource type YAML file with invalid schema
    When I run resource type add with the config file
    Then the resource command should fail

  Scenario: type add with non-rich format
    Given a valid custom resource type YAML file
    When I run resource type add with the config file and format "json"
    Then the resource output should be valid JSON

  # ---- type_remove error paths ----

  Scenario: type remove confirmation prompt aborted
    Given a valid custom resource type YAML file
    And I run resource type add with the config file
    When I run resource type remove "test/custom-type" without yes flag
    Then the resource command should fail

  Scenario: type remove non-existent type raises NotFoundError
    When I run resource type remove "nonexistent/type" with yes flag
    Then the resource command should fail
    And the resource output should contain "not found"

  Scenario: type remove with resources referencing custom type
    Given a valid custom resource type YAML file
    And I run resource type add with the config file
    And I add a resource referencing custom type "test/custom-type"
    When I run resource type remove "test/custom-type" with yes flag
    Then the resource command should fail
    And the resource output should contain "Cannot remove"

  # ---- type_show error paths ----

  Scenario: type show non-rich format
    Given built-in types are bootstrapped
    When I run resource type show "git-checkout" using format "yaml"
    Then the resource output should contain "name"

  Scenario: type show with plain format
    Given built-in types are bootstrapped
    When I run resource type show "git-checkout" using format "plain"
    Then the resource output should contain "git-checkout"

  # ---- type_list non-rich format ----

  Scenario: type list with yaml format
    Given built-in types are bootstrapped
    When I run resource type list with format "yaml"
    Then the resource output should contain "name"

  Scenario: type list with plain format
    Given built-in types are bootstrapped
    When I run resource type list with format "plain"
    Then the resource output should contain "git-checkout"

  # ---- resource_add error paths ----

  Scenario: resource add with non-rich format
    Given built-in types are bootstrapped
    When I run resource add formatted as "json" type "git-checkout" name "local/fmt-test" path "/tmp/fmt"
    Then the resource output should be valid JSON

  Scenario: resource add with description and branch
    Given built-in types are bootstrapped
    When I run resource add with extras type "git-checkout" name "local/branch-test" path "/tmp/b" branch "main" description "My repo"
    Then the resource output should contain "local/branch-test"

  Scenario: resource add with validation error for non-existent type
    When I run resource add "nonexistent" "local/test" with path "/tmp"
    Then the resource command should fail
    And the resource output should contain "not found"

  # ---- resource_list error paths ----

  Scenario: resource list with non-rich format
    Given built-in types are bootstrapped
    And I run resource add "git-checkout" "local/list-fmt" with path "/tmp/lfmt"
    When I run resource list with format "json"
    Then the resource output should be valid JSON

  Scenario: resource list with long description truncation
    Given built-in types are bootstrapped
    And I add a resource with a long description
    When I run resource list
    Then the resource output should contain "local/long-desc"

  # ---- resource_show error paths ----

  Scenario: resource show with non-rich format
    Given built-in types are bootstrapped
    And I run resource add "git-checkout" "local/show-fmt" with path "/tmp/sfmt"
    When I run resource show "local/show-fmt" using format "json"
    Then the resource output should be valid JSON

  Scenario: resource show with empty properties
    Given built-in types are bootstrapped
    And I run resource add "git-checkout" "local/no-props" with path "/tmp/np"
    When I run resource show "local/no-props" using default format
    Then the resource output should contain "local/no-props"

  # ---- resource_remove error paths ----

  Scenario: resource remove confirmation prompt aborted
    Given built-in types are bootstrapped
    And I run resource add "git-checkout" "local/no-confirm" with path "/tmp/nc"
    When I run resource remove "local/no-confirm" without yes flag
    Then the resource command should fail

  Scenario: resource remove non-existent resource raises NotFoundError
    When I run resource remove "nonexistent" with yes flag
    Then the resource command should fail
    And the resource output should contain "not found"

  # ---- CleverAgentsError catch-all paths ----

  Scenario: type list raises CleverAgentsError
    Given a service that raises CleverAgentsError on list_types
    When I run resource type list
    Then the resource command should fail
    And the resource output should contain "Error"

  Scenario: type show raises CleverAgentsError
    Given a service that raises CleverAgentsError on show_type
    When I run resource type show "anything" using default format
    Then the resource command should fail
    And the resource output should contain "Error"

  Scenario: type add raises CleverAgentsError
    Given a valid custom resource type YAML file
    And a service that raises CleverAgentsError on register_type
    When I run resource type add with the config file
    Then the resource command should fail
    And the resource output should contain "Error"

  Scenario: type remove raises CleverAgentsError
    Given a service that raises CleverAgentsError on show_type
    When I run resource type remove "anything" with yes flag
    Then the resource command should fail
    And the resource output should contain "Error"

  Scenario: resource add ValidationError
    Given a service that raises ValidationError on register_resource
    When I run resource add "git-checkout" "local/test" with path "/tmp"
    Then the resource command should fail
    And the resource output should contain "Validation error"

  Scenario: resource add CleverAgentsError
    Given a service that raises CleverAgentsError on register_resource
    When I run resource add "git-checkout" "local/test" with path "/tmp"
    Then the resource command should fail
    And the resource output should contain "Error"

  Scenario: resource list raises CleverAgentsError
    Given a service that raises CleverAgentsError on list_resources
    When I run resource list
    Then the resource command should fail
    And the resource output should contain "Error"

  Scenario: resource show raises CleverAgentsError
    Given a service that raises CleverAgentsError on show_resource
    When I run resource show "anything" using default format
    Then the resource command should fail
    And the resource output should contain "Error"

  Scenario: resource remove raises CleverAgentsError
    Given a service that raises CleverAgentsError on show_resource
    When I run resource remove "anything" with yes flag
    Then the resource command should fail
    And the resource output should contain "Error"

  # ---- Additional path coverage ----

  Scenario: type show with rich format shows panel for type without cli_args
    Given a type without cli_args is registered
    When I run resource type show "test/no-args" using default format
    Then the resource output should contain "test/no-args"

  Scenario: resource show with truly empty properties
    Given built-in types are bootstrapped
    And I add a resource with empty properties
    When I run resource show "local/empty-props" using default format
    Then the resource output should contain "local/empty-props"
    And the resource output should contain "none"

  Scenario: resource remove succeeds with session delete
    Given built-in types are bootstrapped
    And I run resource add "git-checkout" "local/to-delete" with path "/tmp/del"
    When I run resource remove "local/to-delete" with yes flag
    Then the resource output should contain "Removed resource"

  Scenario: type add FileNotFoundError catch
    When I run resource type add with path to missing file
    Then the resource command should fail
    And the resource output should contain "not found"

  # ---- --clone-into flag coverage ----

  @tdd_issue @tdd_issue_4262 @tdd_expected_fail
  Scenario: resource add container-instance with --clone-into stores clone_into property
    Given built-in types are bootstrapped
    When I run resource add "container-instance" "local/clone-test" with clone-into "https://github.com/acme/api.git:/workspace"
    Then the resource output should contain "local/clone-test"
    And the resource output should contain "Added resource"

  Scenario: resource add with --clone-into on non-container type fails
    Given built-in types are bootstrapped
    When I run resource add "git-checkout" "local/bad-clone" with clone-into "https://github.com/acme/api.git:/workspace"
    Then the resource command should fail
    And the resource output should contain "Validation error"

  Scenario: resource add with --clone-into in invalid format fails
    Given built-in types are bootstrapped
    When I run resource add "container-instance" "local/bad-fmt" with clone-into "no-colon-separator"
    Then the resource command should fail
    And the resource output should contain "Validation error"

  Scenario: resource add with --clone-into missing path segment fails
    Given built-in types are bootstrapped
    When I run resource add "container-instance" "local/bad-path" with clone-into "https://github.com/acme/api.git:"
    Then the resource command should fail
    And the resource output should contain "Validation error"
