Feature: TUI session export/import (JSON + Markdown)
  As a TUI user
  I want to export sessions to JSON and Markdown formats
  And import sessions from JSON files via slash commands
  So that I can share, archive, and restore conversation history

  # ---------------------------------------------------------------------------
  # Session domain model: as_export_markdown()
  # ---------------------------------------------------------------------------

  Scenario: Session with messages exports to Markdown
    Given a session with two messages for markdown export
    When I call as_export_markdown on the session
    Then the markdown output should start with "# Session:"
    And the markdown output should contain "## Messages"
    And the markdown output should contain "USER"
    And the markdown output should contain "ASSISTANT"
    And the markdown output should contain "Hello from user"
    And the markdown output should contain "Hello from assistant"

  Scenario: Session with no messages exports to Markdown with placeholder
    Given a session with no messages for markdown export
    When I call as_export_markdown on the session
    Then the markdown output should start with "# Session:"
    And the markdown output should contain "*(no messages)*"

  Scenario: Markdown export includes actor and namespace
    Given a session with actor "openai/gpt-4" for markdown export
    When I call as_export_markdown on the session
    Then the markdown output should contain "**Actor:** openai/gpt-4"
    And the markdown output should contain "**Namespace:** local"

  Scenario: Markdown export includes linked plan IDs
    Given a session with linked plans for markdown export
    When I call as_export_markdown on the session
    Then the markdown output should contain "**Linked Plans:**"

  # ---------------------------------------------------------------------------
  # CLI export command: --format md flag
  # ---------------------------------------------------------------------------

  Scenario: Export session as Markdown to stdout
    Given there is a mocked session for markdown export
    When I run session CLI export with --format md and no output file
    Then the md export CLI result code should be zero
    And the md export CLI output should include "# Session:"

  Scenario: Export session as Markdown to file
    Given there is a mocked session for markdown export
    When I run session CLI export with --format md to a temp file
    Then the md export CLI result code should be zero
    And the exported markdown file should exist
    And the exported markdown file should contain "# Session:"

  @tdd_issue @tdd_issue_4293 @tdd_expected_fail
  Scenario: Export session as JSON (default format)
    Given there is a mocked session for markdown export
    When I run session CLI export with no format flag
    Then the md export CLI result code should be zero
    And the md export CLI output should be parseable as JSON

  Scenario: Export with invalid format flag returns error
    Given there is a mocked session for markdown export
    When I run session CLI export with --format xml
    Then the md export CLI result code should be nonzero
    And the md export CLI output should include "Invalid format"

  # ---------------------------------------------------------------------------
  # TUI command router: /session export and /session import
  # ---------------------------------------------------------------------------

  Scenario: TUI session export command returns success message
    Given a TUI command router with mocked session service for export
    When I call TUI handle with "session export" for the current session
    Then the TUI handle result should contain "Session exported"

  Scenario: TUI session export with path writes file
    Given a TUI command router with mocked session service for export
    When I call TUI handle with "session export /tmp/tui_test_export.json" for the current session
    Then the TUI handle result should contain "Session exported to"
    And the tui exported file "/tmp/tui_test_export.json" should exist

  Scenario: TUI session export with --format md returns success
    Given a TUI command router with mocked session service for export
    When I call TUI handle with "session export --format md" for the current session
    Then the TUI handle result should contain "Session exported (MD)"

  Scenario: TUI session export with invalid format returns error
    Given a TUI command router with mocked session service for export
    When I call TUI handle with "session export --format xml" for the current session
    Then the TUI handle result should contain "Invalid format"

  Scenario: TUI session import with valid file returns success
    Given a TUI command router with mocked session service for import
    And there is a valid JSON export file for TUI import
    When I call TUI handle with "session import <path>" for the import file
    Then the TUI handle result should contain "Session imported:"

  Scenario: TUI session import with no path returns usage
    Given a TUI command router with mocked session service for import
    When I call TUI handle with "session import" for the current session
    Then the TUI handle result should be "Usage: /session:import <path>"

  Scenario: TUI session import with non-existent file returns error
    Given a TUI command router with mocked session service for import
    When I call TUI handle with "session import /tmp/nonexistent_tui_file.json" for the current session
    Then the TUI handle result should contain "File not found"

  Scenario: TUI session import with invalid JSON returns error
    Given a TUI command router with mocked session service for import
    And there is an invalid JSON file for TUI import
    When I call TUI handle with "session import <invalid_path>" for the import file
    Then the TUI handle result should contain "Invalid JSON"

  Scenario: TUI session export with --format txt returns success
    Given a TUI command router with mocked session service for export
    When I call TUI handle with "session export --format txt" for the current session
    Then the TUI handle result should contain "Session exported (TXT)"

  Scenario: TUI session export with --format txt to file writes plain text
    Given a TUI command router with mocked session service for export
    When I call TUI handle with "session export --format txt /tmp/tui_test_export.txt" for the current session
    Then the TUI handle result should contain "Session exported to"
    And the tui exported file "/tmp/tui_test_export.txt" should exist

  Scenario: Plain text export contains session header and messages
    Given a session with two messages for plain text export
    When I call as_export_plain_text on the session
    Then the plain text output should start with "Session:"
    And the plain text output should contain "USER"
    And the plain text output should contain "ASSISTANT"
    And the plain text output should contain "Hello from user"
    And the plain text output should contain "Hello from assistant"

  Scenario: Plain text export with no messages contains placeholder
    Given a session with no messages for plain text export
    When I call as_export_plain_text on the session
    Then the plain text output should start with "Session:"
    And the plain text output should contain "(no messages)"

  Scenario: TUI session export error message includes txt format
    Given a TUI command router with mocked session service for export
    When I call TUI handle with "session export --format xml" for the current session
    Then the TUI handle result should contain "Invalid format"
    And the TUI handle result should contain "txt"
