#!/usr/bin/env bash
# AnyFS Quick Share: End-to-end workflow
# Sender captures agent state, encrypts, uploads, and generates a shareable link.
# Recipient decrypts with only the password.

set -euo pipefail

# ── Configuration ──────────────────────────────────────────────
# Ensure these are set in ~/.anyfs/config.json or as env vars:
#   ANYFS_API_BASE_URL=https://api.anyfs.ai
#   ANYFS_STORAGE_GATEWAY_URL=https://storage.anyfs.ai

# ── Sender Side ────────────────────────────────────────────────

# 1. Initialize (one-time, safe to re-run)
anyfs init

# 2. Quick-share the current workspace
#    - Captures Claude Code agent state
#    - Encrypts each namespace with a unique key
#    - Re-wraps keys with password for sharing
#    - Uploads encrypted files to the AnyFS storage gateway
anyfs quick-share \
  --workspace . \
  --namespaces memory,skills,process \
  --password "MySecretPassword123"

# Output example:
# {
#   "snapshot_id": "snp_ed43b33cac45",
#   "password": "MySecretPassword123",
#   "shares": {
#     "memory": {
#       "cid": "bafybeidbwtgjlz4...",
#       "gateway_url": "https://storage.anyfs.ai/raw/bafybeidbwtgjlz4...",
#       "share_file": ".anyfs/exports/shares/snp_ed43b33cac45.memory.share.json"
#     },
#     "skills": { ... },
#     "process": { ... }
#   },
#   "instructions": "Recipient runs: anyfs share open <file_or_url> -p MySecretPassword123"
# }

# 3. Send to recipient:
#    - The gateway URL (e.g., https://storage.anyfs.ai/raw/<cid>)
#    - The password: "MySecretPassword123"
#    (Use any secure channel: Signal, Slack DM, etc.)

# ── Recipient Side ─────────────────────────────────────────────

# 1. Install AnyFS (one-time)
# pip install anyfs-cli

# 2. Initialize (one-time)
anyfs init

# 3. Decrypt the shared file (no root key needed, only password)
anyfs share open \
  "https://storage.anyfs.ai/raw/3f4f9b..." \
  --password "MySecretPassword123"

# Output:
# {
#   "namespace": "memory",
#   "output_file": "/path/to/memory.json",
#   "item_count": 27
# }

# ── Step-by-Step Alternative ───────────────────────────────────
# For more control, use individual commands:

# Capture agent state
anyfs capture claude-code --workspace .

# Create encrypted snapshot
anyfs snapshot create --workspace .

# Share a specific namespace
anyfs share namespace memory \
  --workspace . \
  --password "AnotherPassword" \
  --snapshot latest

# List all snapshots
anyfs snapshot list --workspace .

# Restore a snapshot locally
anyfs restore --snapshot latest --workspace .
