#!/usr/bin/env bash
# Venture-studio data-room scaffold — deterministic enforcement of the
# scaffold-first principle (Task 286 + feedback_doctrine_paragraph_is_not_a_gate).
#
# Creates exactly one Project (with nine pre-seeded artefact Tasks) in
# the graph, then materialises the ten-section data-room directory tree
# on disk. The venture-studio agent's downstream skills are gated on
# both signals existing — see venture-studio/PLUGIN.md.
#
# Order of operations: graph write first, directories second. The CLI's
# duplicate-name guard makes the script safely re-runnable after any
# partial failure: a second run finds the existing Project, returns its
# id, and retries the mkdir step.
#
# Usage:
#   ACCOUNT_ID=<uuid> scaffold.sh <project-root> <business-name>
#
# Exit codes:
#   0   scaffold complete (or already existed)
#   2   bad arguments / missing ACCOUNT_ID
#   1   graph write failed, directory mkdir failed, or CLI binary missing

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Deployed layout per packages/create-maxy-code/scripts/bundle.js:
#   <install>/premium-plugins/venture-studio/bin/scaffold.sh
#   <install>/platform/plugins/work/mcp/dist/cli/project-create-cli.js
CLI_BIN="${SCRIPT_DIR}/../../../platform/plugins/work/mcp/dist/cli/project-create-cli.js"

if [[ $# -ne 2 ]]; then
  echo "scaffold.sh: usage: ACCOUNT_ID=<uuid> $0 <project-root> <business-name>" >&2
  exit 2
fi

PROJECT_ROOT="$1"
BUSINESS_NAME="$2"

if [[ -z "${ACCOUNT_ID:-}" ]]; then
  echo "scaffold.sh: ACCOUNT_ID env var required" >&2
  exit 2
fi

if [[ ! -d "$PROJECT_ROOT" ]]; then
  echo "scaffold.sh: project root does not exist: $PROJECT_ROOT" >&2
  exit 2
fi

if [[ ! -f "$CLI_BIN" ]]; then
  echo "scaffold.sh: project-create CLI not built or missing at: $CLI_BIN" >&2
  exit 1
fi

DATA_ROOM="${PROJECT_ROOT}/.docs/data-room"

# Nine pre-seeded artefact tasks — order and names match
# premium-plugins/venture-studio/PLUGIN.md § First-conversation routing.
# Section order is preserved so the agent can walk them sequentially.
# Prototype-host is slotted between Stage 2 (wedge + landing copy) and
# Stage 3 (business plan): brand-pack and wedge are hard prerequisites
# (the skill consumes 06-product-ip/brand/ tokens and 01-narrative/LANDING.md);
# hosting before business plan keeps the founder's wedge-validation loop
# (live URL) ahead of investor-pack work.
PAYLOAD=$(cat <<JSON
{
  "accountId": "${ACCOUNT_ID}",
  "name": "${BUSINESS_NAME}",
  "description": "Venture-studio data room for ${BUSINESS_NAME}. Root: ${DATA_ROOM}",
  "tier": "full",
  "workItems": [
    {"name": "Stage 1 — Office-hours design doc", "description": "Produces 01-narrative/office-hours-design.md via the office-hours skill."},
    {"name": "Stage 2 — Wedge validation + landing page + PRD", "description": "Produces 01-narrative/{PMF,LANDING,PRD}.md via the zero-to-prototype skill."},
    {"name": "Brand pack", "description": "Produces brand guidelines + summary + color palette + typography system + tone-of-voice (voice/messaging/copy) + logo guidelines into 06-product-ip/brand/ via the brand-pack skill."},
    {"name": "Prototype host", "description": "Public URL for the landing page and/or wedge prototype via cloudflared ingress + systemd-managed dev server (skill: prototype-host)."},
    {"name": "Stage 3 — Business plan", "description": "Produces 01-narrative/business-plan.md via investor-data-room Stage 3."},
    {"name": "Stage 3b — Term sheet", "description": "Produces html/prospectus/term_sheet.html via investor-data-room Stage 3b."},
    {"name": "Stage 4 — Deck blueprint", "description": "Produces 01-narrative/deck-blueprint.md via investor-data-room Stage 4."},
    {"name": "Stage 4b — Prospectus", "description": "Produces html/prospectus/index.html via investor-data-room Stage 4b."},
    {"name": "Stage 5 — Rendered PDFs", "description": "Produces html/{business-plan,prospectus,deck}/*.pdf via investor-data-room Stage 5."}
  ]
}
JSON
)

# Graph write first. CLI returns {projectId, existing:bool} on stdout;
# on any failure it exits non-zero with a literal stderr message that
# propagates here verbatim — no prose-parsing for control flow
# (memory: feedback_no_stdout_parsing_for_control_flow).
RESULT=$(node "$CLI_BIN" "$PAYLOAD")
echo "scaffold.sh: project-create CLI returned: $RESULT"

# Directories second. The agent's pre-flight gate keys on the existence
# of 01-narrative/ (it AND-checks with project-list); creating it last
# means partial state (graph-only) is naturally invisible to the gate
# until mkdir succeeds.
for section in \
  01-narrative \
  02-corporate-legal \
  03-cap-table \
  04-financials \
  05-team \
  06-product-ip \
  07-customers \
  08-market \
  09-traction-metrics \
  10-supporting; do
  mkdir -p "${DATA_ROOM}/${section}"
done

# Hosted-prototype source roots live alongside the numbered sections under
# data-room/. The prototype-host skill populates this with one subdir per
# surface (landing/, prototype/, etc.); creating the parent here so the
# directory exists before the first invocation.
mkdir -p "${DATA_ROOM}/prototype"

echo "scaffold.sh: data-room scaffolded at ${DATA_ROOM}"
