#!/usr/bin/env bash
set -euo pipefail
#
# prdy-init.sh — Create a new PRD via guided runtime session
#
# Usage:
#   prdy-init.sh [--project <path>] [--name <feature>]
#
# Launches a dedicated runtime session in yolo mode with a specialized
# system prompt that guides the user through PRD creation. The output is
# written to docs/PRD.md.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/_lib.sh"

# ─── Parse arguments ─────────────────────────────────────────────────────────

PROJECT="."
NAME=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --project) PROJECT="$2"; shift 2 ;;
    --name)    NAME="$2"; shift 2 ;;
    -h|--help)
      cat <<'USAGE'
prdy-init.sh — Create a new PRD via guided runtime session

Usage: prdy-init.sh [--project <path>] [--name <feature>]

Options:
  --project <path>   Project directory (default: CWD)
  --name <feature>   Feature or project name (optional, runtime will ask if omitted)

Launches the selected runtime in yolo mode with a PRD-focused prompt.
The agent will ask clarifying questions, then write docs/PRD.md.

Examples:
  mosaic prdy init
  mosaic prdy init --name "User Authentication"
  mosaic prdy init --project ~/src/my-app --name "Dashboard Redesign"
USAGE
      exit 0
      ;;
    *) echo "Unknown option: $1" >&2; exit 1 ;;
  esac
done

# Expand tilde if passed literally (e.g., --project ~/src/foo)
PROJECT="${PROJECT/#\~/$HOME}"

# ─── Preflight checks ───────────────────────────────────────────────────────

RUNTIME_CMD="$(prdy_runtime_command)"
_require_cmd "$RUNTIME_CMD"

# Check for existing PRD
EXISTING="$(find_prd "$PROJECT")"
if [[ -n "$EXISTING" ]]; then
  fail "PRD already exists: $EXISTING"
  echo -e "  Use ${C_CYAN}mosaic prdy update${C_RESET} to modify the existing PRD."
  exit 1
fi

# Ensure docs/ directory exists
mkdir -p "$PROJECT/docs"

# ─── Build system prompt ─────────────────────────────────────────────────────

step "Launching PRD creation session"

SYSTEM_PROMPT="$(build_prdy_system_prompt "init")"

# Build kickoff message
if [[ -n "$NAME" ]]; then
  KICKOFF="Create docs/PRD.md for the feature: ${NAME}. Read the project context first, then ask clarifying questions before writing the PRD."
else
  KICKOFF="Create docs/PRD.md for this project. Read the project context first, then ask the user what they want to build. Ask clarifying questions before writing the PRD."
fi

# ─── Launch runtime ──────────────────────────────────────────────────────────

info "Output target: $PROJECT/$PRD_CANONICAL"
info "Mode: PRD Creation (yolo, runtime: $RUNTIME_CMD)"
echo ""

cd "$PROJECT"
if [[ "$RUNTIME_CMD" == "claude" ]]; then
  exec claude --dangerously-skip-permissions --append-system-prompt "$SYSTEM_PROMPT" "$KICKOFF"
fi

if [[ "$RUNTIME_CMD" == "codex" ]]; then
  CODEX_PROMPT="$(cat <<EOF
Follow this PRD contract exactly.

$SYSTEM_PROMPT

Task:
$KICKOFF
EOF
)"
  exec codex --dangerously-bypass-approvals-and-sandbox "$CODEX_PROMPT"
fi

fail "Unsupported runtime: $RUNTIME_CMD"
exit 1
