#!/bin/bash

# Use the persona-based version for better question generation
exec "$(dirname "$0")/claudefsd-analyze-brief-personas" "$@"

mkdir -p logs

# Use a temporary directory for tmp files, as codex is sandboxed to this directory
mkdir -p tmp
export TMPDIR=tmp/

# look for a brief file (prefer docs/BRIEF.md, fallback to BRIEF.md)
source "$(dirname "$0")/claudefsd-find-brief"
BRIEF_FILE=$(find_brief_file)
if [ $? -ne 0 ]; then
    echo "No BRIEF.md file found in docs/ or root directory, please create one first"
    exit 1
fi

if [ "$EDITOR" == "" ]; then
    EDITOR="nano"
fi

LOGFILE="logs/claude-$(date +%Y%m%d_%H%M%S).txt"

echo -e "\033[32m==================================================================\033[0m"
echo -e "\033[32m== ANALYZING PROJECT BRIEF\033[0m"
echo -e "\033[32m==================================================================\033[0m"

prompt1="
Read all of these documents if they exist:
- $BRIEF_FILE -- the project brief
- docs/CLAUDE-NOTES.md -- AI's working notes and understanding
- docs/PLAN.md -- the project plan
- docs/QUESTIONS.md -- the project questions
- docs/IDEAS.md -- the backlog of future ideas
- docs/WEBTESTS.md -- the project web tests
- README.md -- the project README

Your job, as a megathinker business analyst, is to analyze the project brief and generate clarifying questions.

1. Read through the BRIEF.md and any existing documents to understand the project.
2. Generate 10-15 relevant questions to clarify ambiguous aspects of the brief.
3. Create or update docs/QUESTIONS.md with these questions.
4. Focus on questions that would help an AI developer avoid making incorrect assumptions.

Types of questions to include:
- Technical architecture and platform choices
- User experience and interface requirements
- Data requirements and integrations
- Performance and scalability expectations
- Security and compliance needs
- Deployment and operational requirements

DO NOT answer the questions yourself - just generate them for the user to answer.
"

# run BA's
echo "Running claude with opus model..."
claude --model opus --dangerously-skip-permissions -p "$prompt1" | tee >(cat > $LOGFILE-ba1)

# Only run codex if available
if command -v codex >/dev/null 2>&1; then
    echo "Running codex o3 (results won't display)..."
    codex -m o3 --full-auto -q "$prompt1" > $LOGFILE-ba2
else
    echo "Warning: codex not found, skipping enhanced analysis"
    echo "Codex not available, skipping o3 analysis" > $LOGFILE-ba2
fi

echo -e "\033[32m==================================================================\033[0m"
echo -e "\033[32m== ANALYSIS COMPLETE\033[0m"
echo -e "\033[32m==================================================================\033[0m"
echo "Questions have been generated in docs/QUESTIONS.md"
echo "Please answer these questions before proceeding to create the plan."
