#!/bin/bash
# ============================================================
# Fogbreak — Build All Instructions via Claude Code Interactive
# Usage: chmod +x build-all.sh && ./build-all.sh
# Resume: ./build-all.sh 03  (starts from instruction 03)
# ============================================================

cd ~/Documents/Claude/Projects/Fogbreak.io || exit 1

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

INSTRUCTIONS=(
  01-GEOGRAPHIC-EXTRACTION
  02-DATABASE-REDESIGN
  03-SELF-HOSTED-AI
  04-FOLLOW-UP-BOSS-REPLACEMENT
  05-SHOWINGTIME-REPLACEMENT
  06-SISU-REPLACEMENT
  07-PAPERLESS-PIPELINE-REPLACEMENT
  08-DOCUSIGN-REPLACEMENT
  09-REALSCOUT-REPLACEMENT
  10-AI-COPY-ENGINE
  11-SOCIAL-MEDIA-AUTOMATION
  12-VIRTUAL-STAGING
  13-VIDEO-REEL-GENERATION
  14-PHOTO-ENHANCEMENT
  15-NEXTJS-WEB-APP
  16-REACT-NATIVE-MOBILE
  17-CLIENT-PORTAL
  18-MLS-INTEGRATION
  19-SMS-TWILIO
  20-ADVANCED-ANALYTICS
  21-SELF-HOST-MIGRATION
  22-VENDOR-MARKETPLACE
  23-DATA-MIGRATION
)

START_FROM="${1:-01}"

echo ""
echo -e "${GREEN}═══════════════════════════════════════════${NC}"
echo -e "${GREEN}  FOGBREAK — BUILDING ALL INSTRUCTIONS${NC}"
echo -e "${GREEN}═══════════════════════════════════════════${NC}"
echo ""

for INST in "${INSTRUCTIONS[@]}"; do
  NUM="${INST%%-*}"

  if [ "$NUM" -lt "$START_FROM" ] 2>/dev/null; then
    continue
  fi

  echo -e "${YELLOW}──────────────────────────────────────${NC}"
  echo -e "${YELLOW}  Building: $INST${NC}"
  echo -e "${YELLOW}──────────────────────────────────────${NC}"

  echo "Read CLAUDE.md first for project context. Then read claude-code-instructions/${INST}.md and execute every instruction in it. Build all files specified with complete, production-ready code. All queries use PDO prepared statements. All queries filter by tenant_id. Zero geographic hardcoding. Do not ask questions — just build." | claude --dangerously-skip-permissions

  EXIT_CODE=$?

  if [ $EXIT_CODE -ne 0 ]; then
    echo -e "${RED}  ✗ $INST failed (exit $EXIT_CODE)${NC}"
    echo -e "${RED}  Resume: ./build-all.sh $NUM${NC}"
    exit 1
  fi

  echo -e "${GREEN}  ✓ $INST complete${NC}"

  # Auto-commit
  if [ -n "$(git status --porcelain)" ]; then
    git add -A
    git commit -m "Build: $INST

Automated build via Fogbreak build-all.sh
Co-Authored-By: Claude <noreply@anthropic.com>"
    echo -e "${GREEN}  ✓ Committed${NC}"
  fi

  echo ""
done

# Push everything
echo -e "${GREEN}  Pushing to GitHub...${NC}"
git push origin main
echo ""
echo -e "${GREEN}═══════════════════════════════════════════${NC}"
echo -e "${GREEN}  ALL INSTRUCTIONS COMPLETE${NC}"
echo -e "${GREEN}═══════════════════════════════════════════${NC}"
