#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
cd "$ROOT_DIR"

SANDBOX="$ROOT_DIR/sandbox/pi-tool-fixtures"
UUID="22222222-3333-4444-5555-666666666666"
OWNER="agent1"
PEER="agent2"
REQUEST_ID="req-$UUID"
RUN_ACTION_ID="act-pi-run-real-$UUID"
RELEASE_ACTION_ID="act-pi-release-$UUID"
RUN_ID="run-pi-run-real-$UUID"
RUN_STDOUT="$SANDBOX/shared/.rho/runs/$RUN_ID/stdout.txt"
RELEASE_OUTPUT="$SANDBOX/shared/results/$REQUEST_ID/output.txt"

rm -rf "$SANDBOX"
mkdir -p "$SANDBOX/shared/workspace" "$SANDBOX/agent-control/outbox" "$SANDBOX/host-control/outbox" "$SANDBOX/host-control/inbox"

cargo run --quiet --bin rho -- dataset \
  --name prices-pi-fixture-test \
  --real prototype/tests/scenarios/two-console-demo/assets/datasets/prices-real.csv \
  --mock prototype/tests/scenarios/two-console-demo/assets/datasets/prices-mock.csv \
  --owner "$OWNER" \
  --share-dir "$SANDBOX/users/$OWNER/datasets/share" \
  --private-dir "$SANDBOX/users/$OWNER/datasets/private" \
  --uuid "$UUID" \
  --description "Pi tool fixture e2e test"

cargo run --quiet --bin rho -- publish \
  "$OWNER" "$UUID" \
  --source-root "$SANDBOX/users" \
  --target-root "$SANDBOX/shared/datasets"

cargo run --quiet --bin rho -- tools install-minimal \
  --shared-root "$SANDBOX/shared" \
  --owner "$OWNER"

cp prototype/tests/scenarios/two-console-demo/assets/workspace/sum_prices.py "$SANDBOX/shared/workspace/sum_prices.py"

cargo run --quiet --bin rho -- request create-run \
  --shared-root "$SANDBOX/shared" \
  --id "$REQUEST_ID" \
  --from "$PEER" \
  --to "$OWNER" \
  --tool-id run_real \
  --dataset-uuid "$UUID" \
  --code-path "$SANDBOX/shared/workspace/sum_prices.py" \
  --command "python3 sum_prices.py DATASET_CSV" \
  --tier real

cat > "$SANDBOX/agent-control/outbox/$RUN_ACTION_ID.json" <<EOF
{
  "version": 1,
  "kind": "controlled_action",
  "action": {
    "action_id": "$RUN_ACTION_ID",
    "request_id": "$REQUEST_ID",
    "tool_id": "run_real",
    "requested_by": "$PEER",
    "requested_for": "$OWNER",
    "action_type": "run_real_data",
    "summary": "Run the generated price summing script on real data.",
    "reason": "The requester has created code against mock data and needs the protected real aggregate.",
    "script_path": "/output/sum_prices.py",
    "output_path": "/results/private/stdout.txt"
  }
}
EOF

cargo run --quiet --bin rho -- run import-action \
  --agent-control-root "$SANDBOX/agent-control" \
  --control-root "$SANDBOX/host-control" \
  --action-id "$RUN_ACTION_ID" \
  --map "/output=$SANDBOX/shared/workspace" \
  --map "/results/private=$SANDBOX/shared/.rho/runs/$RUN_ID"

grep -q "$SANDBOX/shared/workspace/sum_prices.py" "$SANDBOX/host-control/outbox/$RUN_ACTION_ID.json"
grep -q "$RUN_STDOUT" "$SANDBOX/host-control/outbox/$RUN_ACTION_ID.json"

cargo run --quiet --bin rho -- run grant-action \
  --shared-root "$SANDBOX/shared" \
  --control-root "$SANDBOX/host-control" \
  --action-id "$RUN_ACTION_ID" \
  --granted-by "$OWNER" \
  --input "code:$SANDBOX/shared/workspace/sum_prices.py"

RUN_STATUS_PATH="$(RHO_SANDBOX_RUNNER=fake cargo run --quiet --bin rho -- run controlled-action \
  --shared-root "$SANDBOX/shared" \
  --control-root "$SANDBOX/host-control" \
  --action-id "$RUN_ACTION_ID" \
  --private-root "$SANDBOX/users/$OWNER/datasets/private")"
grep -q "completed" "$RUN_STATUS_PATH"
test "$(cat "$RUN_STDOUT")" = "1113.03"

cat > "$SANDBOX/agent-control/outbox/$RELEASE_ACTION_ID.json" <<EOF
{
  "version": 1,
  "kind": "controlled_action",
  "action": {
    "action_id": "$RELEASE_ACTION_ID",
    "request_id": "$REQUEST_ID",
    "tool_id": "publish_results",
    "requested_by": "$OWNER",
    "requested_for": "$OWNER",
    "action_type": "release_results",
    "summary": "Release the approved aggregate result.",
    "reason": "The protected run completed and the aggregate should be written back to shared results.",
    "input_path": "/results/private/stdout.txt",
    "output_path": "/release/shared/output.txt"
  }
}
EOF

cargo run --quiet --bin rho -- run import-action \
  --agent-control-root "$SANDBOX/agent-control" \
  --control-root "$SANDBOX/host-control" \
  --action-id "$RELEASE_ACTION_ID" \
  --map "/results/private=$SANDBOX/shared/.rho/runs/$RUN_ID" \
  --map "/release/shared=$SANDBOX/shared/results/$REQUEST_ID"

cargo run --quiet --bin rho -- run grant-action \
  --shared-root "$SANDBOX/shared" \
  --control-root "$SANDBOX/host-control" \
  --action-id "$RELEASE_ACTION_ID" \
  --granted-by "$OWNER" \
  --input "artifact:$RUN_STDOUT"

RELEASE_STATUS_PATH="$(cargo run --quiet --bin rho -- run controlled-action \
  --shared-root "$SANDBOX/shared" \
  --control-root "$SANDBOX/host-control" \
  --action-id "$RELEASE_ACTION_ID" \
  --private-root "$SANDBOX/users/$OWNER/datasets/private")"
grep -q "completed" "$RELEASE_STATUS_PATH"
test "$(cat "$RELEASE_OUTPUT")" = "1113.03"

echo "inspect sandbox: $SANDBOX"
find "$SANDBOX/agent-control" "$SANDBOX/host-control" "$SANDBOX/shared/.rho" -maxdepth 3 -type f | sort
echo "pi tool fixture e2e passed"
