#!/bin/bash
# Quick Start Script for Search & Admin Integration Testing
# Run this after setting admin role in Firestore

set -e  # Exit on error

echo "🚀 MyRoadTrip - Search & Admin Integration Tester"
echo "=================================================="
echo ""

# Check if we're in project root
if [ ! -f "pnpm-workspace.yaml" ]; then
  echo "❌ Error: Run this script from project root"
  exit 1
fi

echo "📦 Step 1: Installing dependencies..."
pnpm install
echo "✅ Dependencies installed"
echo ""

echo "🔍 Step 2: Type checking..."
pnpm type-check || echo "⚠️  Type errors exist (non-blocking)"
echo ""

echo "🧹 Step 3: Linting..."
pnpm lint || echo "⚠️  Lint warnings exist (non-blocking)"
echo ""

echo "🔥 Step 4: Checking Firebase config..."
if [ ! -f "apps/api/serviceAccount.json" ]; then
  echo "⚠️  Warning: apps/api/serviceAccount.json not found"
  echo "   Place your Firebase service account JSON here"
else
  echo "✅ Firebase service account found"
fi
echo ""

echo "📊 Step 5: Checking Redis..."
if command -v redis-cli &> /dev/null; then
  if redis-cli ping &> /dev/null; then
    echo "✅ Redis is running"
  else
    echo "⚠️  Redis not running. Starting..."
    if [[ "$OSTYPE" == "darwin"* ]]; then
      brew services start redis
    else
      sudo systemctl start redis
    fi
  fi
else
  echo "⚠️  Redis CLI not found. Install: brew install redis"
fi
echo ""

echo "📝 Step 6: Integration checklist..."
echo ""
echo "MANUAL STEPS YOU MUST DO:"
echo "========================="
echo ""
echo "1. SET ADMIN ROLE (CRITICAL)"
echo "   → Go to Firebase Console"
echo "   → Firestore Database → users collection"
echo "   → Find your user (by UID)"
echo "   → Add field: role = \"admin\""
echo ""
echo "2. START SERVERS (2 terminals)"
echo "   Terminal 1: cd apps/api && pnpm dev"
echo "   Terminal 2: cd apps/web && pnpm dev"
echo ""
echo "3. TEST FRONTEND"
echo "   → Open http://localhost:5173"
echo "   → Log in with your user"
echo "   → Click 'Search' in navigation"
echo "   → Click 'Admin' (should show dashboard if role set)"
echo ""
echo "4. TEST BACKEND (get auth token from browser first)"
echo "   → DevTools → Application → Local Storage"
echo "   → Copy Firebase token"
echo "   → Run: curl http://localhost:3000/api/admin/stats -H 'Authorization: Bearer TOKEN'"
echo ""

echo "📖 Full details in: INTEGRATION_FINAL_STATUS.md"
echo ""
echo "✅ Setup complete. Ready for manual testing!"
echo ""
echo "To start servers:"
echo "  Terminal 1: cd apps/api && pnpm dev"
echo "  Terminal 2: cd apps/web && pnpm dev"
