#!/bin/bash
# Bootstrap agent-state directories for all teleo agents.
# Run once on VPS: bash ops/agent-state/bootstrap.sh
# Safe to re-run — skips existing files, only creates missing ones.

set -euo pipefail

STATE_ROOT="${TELEO_STATE_ROOT:-/opt/teleo-eval/agent-state}"

AGENTS=("rio" "clay" "theseus" "vida" "astra" "leo")
DOMAINS=("internet-finance" "entertainment" "ai-alignment" "health" "space-development" "grand-strategy")

log() { echo "[$(date -Iseconds)] $*"; }

for i in "${!AGENTS[@]}"; do
    AGENT="${AGENTS[$i]}"
    DOMAIN="${DOMAINS[$i]}"
    DIR="$STATE_ROOT/$AGENT"

    log "Bootstrapping $AGENT..."
    mkdir -p "$DIR/inbox"

    # report.json — current status
    if [ ! -f "$DIR/report.json" ]; then
        cat > "$DIR/report.json" <<EOJSON
{
  "agent": "$AGENT",
  "updated_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "status": "idle",
  "summary": "State initialized — no sessions recorded yet.",
  "current_task": null,
  "last_session": null,
  "blocked_by": null,
  "next_priority": null
}
EOJSON
        log "  Created report.json"
    fi

    # tasks.json — empty task queue
    if [ ! -f "$DIR/tasks.json" ]; then
        cat > "$DIR/tasks.json" <<EOJSON
{
  "agent": "$AGENT",
  "updated_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "tasks": []
}
EOJSON
        log "  Created tasks.json"
    fi

    # session.json — no session yet
    if [ ! -f "$DIR/session.json" ]; then
        cat > "$DIR/session.json" <<EOJSON
{
  "agent": "$AGENT",
  "session_id": null,
  "started_at": null,
  "ended_at": null,
  "type": null,
  "domain": "$DOMAIN",
  "branch": null,
  "status": "idle",
  "model": null,
  "timeout_seconds": null,
  "research_question": null,
  "belief_targeted": null,
  "disconfirmation_target": null,
  "sources_archived": 0,
  "sources_expected": 0,
  "tokens_used": null,
  "cost_usd": null,
  "errors": [],
  "handoff_notes": null
}
EOJSON
        log "  Created session.json"
    fi

    # memory.md — empty operational memory
    if [ ! -f "$DIR/memory.md" ]; then
        cat > "$DIR/memory.md" <<EOMD
# ${AGENT^} — Operational Memory

## Cross-Session Patterns
(none yet)

## Dead Ends
(none yet)

## Open Questions
(none yet)

## Corrections
(none yet)

## Cross-Agent Flags Received
(none yet)
EOMD
        log "  Created memory.md"
    fi

    # metrics.json — zero counters
    if [ ! -f "$DIR/metrics.json" ]; then
        cat > "$DIR/metrics.json" <<EOJSON
{
  "agent": "$AGENT",
  "updated_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "lifetime": {
    "sessions_total": 0,
    "sessions_completed": 0,
    "sessions_timeout": 0,
    "sessions_error": 0,
    "sources_archived": 0,
    "claims_proposed": 0,
    "claims_accepted": 0,
    "claims_challenged": 0,
    "claims_rejected": 0,
    "disconfirmation_attempts": 0,
    "disconfirmation_hits": 0,
    "cross_agent_flags_sent": 0,
    "cross_agent_flags_received": 0
  },
  "rolling_30d": {
    "sessions": 0,
    "sources_archived": 0,
    "claims_proposed": 0,
    "acceptance_rate": 0.0,
    "avg_sources_per_session": 0.0
  }
}
EOJSON
        log "  Created metrics.json"
    fi

    # journal.jsonl — empty log
    if [ ! -f "$DIR/journal.jsonl" ]; then
        echo "{\"ts\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"event\":\"state_initialized\",\"schema_version\":\"1.0\"}" > "$DIR/journal.jsonl"
        log "  Created journal.jsonl"
    fi

done

log "Bootstrap complete. State root: $STATE_ROOT"
log "Agents initialized: ${AGENTS[*]}"
