#!/usr/bin/env bash
# SessionStart hook: Scaffold .codeflash/{teammember}/{org}/{project}/ if it doesn't exist.
# Infers org/project from git remote origin. Team member is derived from
# git user name. File generation is delegated to scripts/scaffold.sh.

cd "$CLAUDE_PROJECT_DIR" 2>/dev/null || exit 0

CF_DIR="$CLAUDE_PROJECT_DIR/.codeflash"
SCAFFOLD="$CLAUDE_PROJECT_DIR/scripts/scaffold.sh"

# Derive team member from git user (lowercase, no spaces)
MEMBER=$(git config user.name 2>/dev/null | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
[ -z "$MEMBER" ] && MEMBER="unknown"

# Parse git remote origin
REMOTE=$(git remote get-url origin 2>/dev/null)
if [ -z "$REMOTE" ]; then
    if [ -d "$CF_DIR" ]; then
        exit 0
    fi
    cat <<EOF
{
  "systemMessage": "No .codeflash/ directory found and no git remote origin to infer org/project. Ask the user for the organization and project name, then run: bash scripts/scaffold.sh <org> <project> .codeflash/$MEMBER/<org>/<project>"
}
EOF
    exit 0
fi

# Extract org/project from common remote formats:
#   git@github.com:org/project.git
#   https://github.com/org/project.git
#   ssh://git@github.com/org/project.git
ORG=""
PROJECT=""

if echo "$REMOTE" | grep -qE '^git@'; then
    PATH_PART=$(echo "$REMOTE" | sed -E 's/^git@[^:]*://' | sed 's/\.git$//')
    ORG=$(echo "$PATH_PART" | cut -d'/' -f1)
    PROJECT=$(echo "$PATH_PART" | cut -d'/' -f2)
elif echo "$REMOTE" | grep -qE '^https?://'; then
    PATH_PART=$(echo "$REMOTE" | sed -E 's|^https?://[^/]*/||' | sed 's/\.git$//')
    ORG=$(echo "$PATH_PART" | cut -d'/' -f1)
    PROJECT=$(echo "$PATH_PART" | cut -d'/' -f2)
elif echo "$REMOTE" | grep -qE '^ssh://'; then
    PATH_PART=$(echo "$REMOTE" | sed -E 's|^ssh://[^/]*/||' | sed 's/\.git$//')
    ORG=$(echo "$PATH_PART" | cut -d'/' -f1)
    PROJECT=$(echo "$PATH_PART" | cut -d'/' -f2)
fi

# Lowercase org and project
ORG=$(echo "$ORG" | tr '[:upper:]' '[:lower:]')
PROJECT=$(echo "$PROJECT" | tr '[:lower:]' '[:lower:]')

if [ -z "$ORG" ] || [ -z "$PROJECT" ]; then
    if [ -d "$CF_DIR" ]; then
        exit 0
    fi
    cat <<EOF
{
  "systemMessage": "No .codeflash/ directory found. Could not parse org/project from git remote. Ask the user for the organization and project name, then run: bash scripts/scaffold.sh <org> <project> .codeflash/$MEMBER/<org>/<project>"
}
EOF
    exit 0
fi

PROJECT_DIR="$CF_DIR/$MEMBER/$ORG/$PROJECT"

# Skip bootstrap when working on the agent repo itself
if [ "$ORG" = "codeflash-ai" ] && [ "$PROJECT" = "codeflash-agent" ]; then
    exit 0
fi

# Ensure observability dir exists
mkdir -p "$CF_DIR/observability"

# Already initialized — tell Claude to read the existing files
if [ -d "$PROJECT_DIR" ]; then
    cat <<EOF
{
  "systemMessage": "Read $PROJECT_DIR/status.md and $PROJECT_DIR/data/results.tsv to understand current project state before starting work. Benchmark scripts are in $PROJECT_DIR/bench/, VM infra is in $PROJECT_DIR/infra/."
}
EOF
    exit 0
fi

# Scaffold using the shared generator
if [ -x "$SCAFFOLD" ]; then
    bash "$SCAFFOLD" "$ORG" "$PROJECT" "$PROJECT_DIR"
else
    echo "Warning: $SCAFFOLD not found, cannot scaffold" >&2
    exit 0
fi

cat <<EOF
{
  "systemMessage": "Scaffolded $PROJECT_DIR/ with status.md, bench/, data/results.tsv, infra/cloud-init.yaml, and infra/vm-manage.sh. Fill in: $PROJECT_DIR/status.md with current project state, completed work, next steps, and blockers; $PROJECT_DIR/data/results.tsv with benchmark results as they are collected; $PROJECT_DIR/bench/ with project-specific benchmark scripts; $PROJECT_DIR/infra/cloud-init.yaml with project-specific setup and benchmark file entries; $PROJECT_DIR/infra/vm-manage.sh is ready to use -- run 'bash $PROJECT_DIR/infra/vm-manage.sh create' to provision."
}
EOF

exit 0
