setup:
  cleanup_on_exit: false
  default_mode: default
  env:
    telegram_profile: RHO_TELEGRAM_PROFILE
    telegram_bot_token: RHO_TELEGRAM_BOT_TOKEN
    telegram_chat_id: RHO_TELEGRAM_CHAT_ID
    telegram_user_id: RHO_TELEGRAM_USER_ID
  generate:
    dataset_uuid: uuid
  vars:
    owner: test-agent1
    peer: test-agent2
  paths:
    sandbox_dir: "{{repo_root}}/sandbox/two-console-demo"
    request_id: "req-{{dataset_uuid}}"
    result_id: "result-{{dataset_uuid}}"
    users_root: "{{sandbox_dir}}/users"
    shared_root: "{{sandbox_dir}}/shared"
    share_stage_dir: "{{users_root}}/{{owner}}/datasets/share/{{dataset_uuid}}"
    private_stage_dir: "{{users_root}}/{{owner}}/datasets/private/{{dataset_uuid}}"
    published_dir: "{{shared_root}}/datasets/{{dataset_uuid}}"
    request_path: "{{shared_root}}/.rho/requests/{{request_id}}.yaml"
    inbox_agent1: "{{shared_root}}/.rho/inbox/agent1/msg-{{dataset_uuid}}.yaml"
    inbox_agent2_status: "{{shared_root}}/.rho/inbox/agent2/status-{{dataset_uuid}}.yaml"
    inbox_agent2_result: "{{shared_root}}/.rho/inbox/agent2/result-{{dataset_uuid}}.yaml"
    result_manifest: "{{shared_root}}/.rho/results/{{request_id}}.yaml"
    result_output_dir: "{{shared_root}}/results/{{request_id}}"
    result_output: "{{result_output_dir}}/output.txt"
    workspace_dir: "{{shared_root}}/workspace"
    workspace_script: "{{workspace_dir}}/sum_prices.py"
    real_csv: "{{private_stage_dir}}/real/prices-real.csv"
    mock_csv: "{{published_dir}}/mock/prices-mock.csv"
  cleanup:
    - "{{sandbox_dir}}"

steps:
  - name: Create twin dataset
    actor: "{{owner}}"
    summary: Create the owner's private and share-stage dataset copies inside the sandbox.
    run: |
      ./rho-dataset \
        --name prices-demo-test \
        --real "{{assets_dir}}/datasets/prices-real.csv" \
        --mock "{{assets_dir}}/datasets/prices-mock.csv" \
        --owner "{{owner}}" \
        --share-dir "{{users_root}}/{{owner}}/datasets/share" \
        --private-dir "{{users_root}}/{{owner}}/datasets/private" \
        --uuid "{{dataset_uuid}}" \
        --description "Automated two-console demo test"
    assert_files:
      - "{{share_stage_dir}}/dataset.yaml"
      - "{{private_stage_dir}}/dataset.yaml"
      - "{{share_stage_dir}}/mock/prices-mock.csv"
      - "{{private_stage_dir}}/real/prices-real.csv"

  - name: Publish mock twin
    actor: "{{owner}}"
    summary: Publish the mock twin into shared space for the peer to develop against.
    run: |
      ./publish "{{owner}}" "{{dataset_uuid}}" \
        --source-root "{{users_root}}" \
        --target-root "{{shared_root}}/datasets"
    assert_files:
      - "{{published_dir}}/dataset.yaml"
      - "{{mock_csv}}"

  - name: Stage workspace script
    actor: "{{peer}}"
    summary: Place the shared workspace script that will run against mock and real datasets.
    mkdirs:
      - "{{workspace_dir}}"
    copy:
      - from: "{{assets_dir}}/workspace/sum_prices.py"
        to: "{{workspace_script}}"
        mode: "0755"
    assert_files:
      - "{{workspace_script}}"

  - name: Run against published mock data
    actor: "{{peer}}"
    summary: Execute the shared workspace script against the published mock dataset.
    run: python3 "{{workspace_script}}" "{{mock_csv}}"
    capture: mock_output
    expect_stdout: "555.00"

  - name: Create manual request and inbox message
    actor: "{{peer}}"
    summary: Write the canonical request manifest and the owner-facing inbox notice into shared .rho state.
    render:
      - template: "{{assets_dir}}/request.yaml.tmpl"
        to: "{{request_path}}"
      - template: "{{assets_dir}}/inbox-agent1.yaml.tmpl"
        to: "{{inbox_agent1}}"
    assert_files:
      - "{{request_path}}"
      - "{{inbox_agent1}}"

  - name: Check request starts pending
    actor: test-runner
    summary: Confirm the new request is initially pending.
    run: ./rho request status "{{request_id}}" --shared-root "{{shared_root}}"
    expect_stdout: "pending"

  - name: Approve request from CLI
    actor: test-runner
    summary: Approve the pending request through the generic approval CLI.
    when: default
    run: |
      ./rho approve "{{request_id}}" \
        --shared-root "{{shared_root}}" \
        --decision approve \
        --actor test-runner \
        --note "Approved by automated test."
    expect_stdout: "approved"

  - name: Initialize Telegram profile
    actor: "{{owner}}"
    summary: Initialize the owner's Telegram adapter profile inside the sandbox.
    when: live
    run: |
      test -n "{{telegram_profile}}"
      test -n "{{telegram_bot_token}}"
      test -n "{{telegram_chat_id}}"
      test -n "{{telegram_user_id}}"
      RHO_TELEGRAM_ROOT="{{sandbox_dir}}" \
      ./rho-telegram init "{{telegram_profile}}" \
        --user "{{owner}}" \
        --token "{{telegram_bot_token}}" \
        --chat-id "{{telegram_chat_id}}"

  - name: Bind Telegram profile
    actor: "{{owner}}"
    summary: Bind the Telegram profile to the configured chat and operator identity.
    when: live
    run: |
      RHO_TELEGRAM_ROOT="{{sandbox_dir}}" \
      ./rho-telegram bind "{{telegram_profile}}" \
        --user "{{owner}}" \
        --chat-id "{{telegram_chat_id}}" \
        --user-id "{{telegram_user_id}}"

  - name: Drain old Telegram updates
    actor: test-runner
    summary: Clear stale Telegram updates before sending a fresh approval request.
    when: live
    run: |
      RHO_TELEGRAM_ROOT="{{sandbox_dir}}" \
      ./rho-telegram drain "{{telegram_profile}}" --user "{{owner}}"

  - name: Send Telegram approval request
    actor: "{{owner}}"
    summary: Send the approval request through the Telegram middleware adapter.
    when: live
    run: |
      RHO_TELEGRAM_ROOT="{{sandbox_dir}}" \
      ./rho-telegram send-approval-request "{{telegram_profile}}" \
        --user "{{owner}}" \
        --shared-root "{{shared_root}}" \
        --request-id "{{request_id}}"

  - name: Wait for Telegram approval action
    actor: operator
    summary: Wait for a human approval or denial action to arrive through Telegram.
    when: live
    run: |
      RHO_TELEGRAM_ROOT="{{sandbox_dir}}" \
      ./rho-telegram approval-listen "{{telegram_profile}}" \
        --user "{{owner}}" \
        --shared-root "{{shared_root}}" \
        --once \
        --timeout-seconds 90

  - name: Read request decision
    actor: test-runner
    summary: Read the recorded request status after CLI or Telegram approval handling.
    run: ./rho request status "{{request_id}}" --shared-root "{{shared_root}}"
    capture: request_status

  - name: Assert request approved
    actor: test-runner
    summary: Stop the scenario unless the operator decision recorded an approval.
    run: |
      case "{{request_status}}" in
        approved)
          exit 0
          ;;
        denied)
          echo "request {{request_id}} was denied by the operator" >&2
          exit 1
          ;;
        pending)
          echo "request {{request_id}} is still pending; no approval action was recorded" >&2
          exit 1
          ;;
        *)
          echo "request {{request_id}} has unexpected status: {{request_status}}" >&2
          exit 1
          ;;
      esac

  - name: Write mock-finished status update
    actor: "{{owner}}"
    summary: Notify the peer that the mock run phase is complete and real execution is next.
    render:
      - template: "{{assets_dir}}/inbox-agent2-status.yaml.tmpl"
        to: "{{inbox_agent2_status}}"
    assert_files:
      - "{{inbox_agent2_status}}"
    assert_contains:
      - path: "{{inbox_agent2_status}}"
        pattern: "mock_run_finished"

  - name: Run against private real data
    actor: "{{owner}}"
    summary: Execute the same workspace script against the private real dataset.
    run: python3 "{{workspace_script}}" "{{real_csv}}"
    capture: real_output
    expect_stdout: "1113.03"

  - name: Publish result and notify agent2
    actor: "{{owner}}"
    summary: Publish the real run output and notify the peer that results are available.
    mkdirs:
      - "{{result_output_dir}}"
    run: printf '%s\n' '{{real_output}}' > "{{result_output}}"
    render:
      - template: "{{assets_dir}}/result-manifest.yaml.tmpl"
        to: "{{result_manifest}}"
      - template: "{{assets_dir}}/inbox-agent2-result.yaml.tmpl"
        to: "{{inbox_agent2_result}}"
    assert_files:
      - "{{result_output}}"
      - "{{result_manifest}}"
      - "{{inbox_agent2_result}}"
    assert_contains:
      - path: "{{result_manifest}}"
        pattern: "released"
      - path: "{{result_output}}"
        pattern: "1113.03"
