#!/bin/bash

# FitAI - Clerk Dependencies Installation Script
# This script installs all required dependencies for Clerk authentication in the mobile app

set -e  # Exit on error

echo "=========================================="
echo "FitAI - Installing Clerk Dependencies"
echo "=========================================="
echo ""

# Check if we're in the mobile app directory
if [ ! -f "package.json" ]; then
    echo "❌ Error: package.json not found!"
    echo "Please run this script from the apps/mobile directory:"
    echo "  cd apps/mobile && bash ../../install-clerk-deps.sh"
    exit 1
fi

# Check if this is the mobile app
if ! grep -q "@fitai/mobile" package.json; then
    echo "❌ Error: This doesn't appear to be the mobile app directory!"
    echo "Please run from apps/mobile"
    exit 1
fi

echo "📦 Installing Clerk and required Expo dependencies..."
echo ""

# Install all dependencies
npm install \
    @clerk/clerk-expo \
    expo-web-browser \
    expo-auth-session \
    expo-secure-store \
    expo-crypto \
    react-dom \
    react-native-web

echo ""
echo "✅ Installation complete!"
echo ""
echo "📋 Installed packages:"
echo "  - @clerk/clerk-expo (Core Clerk SDK)"
echo "  - expo-web-browser (OAuth flows)"
echo "  - expo-auth-session (SSO and sessions)"
echo "  - expo-secure-store (Secure token storage)"
echo "  - expo-crypto (Cryptographic functions)"
echo "  - react-dom (React DOM for web compatibility)"
echo "  - react-native-web (React Native web compatibility)"
echo ""

# Verify installation
echo "🔍 Verifying installation..."
echo ""

if npm list @clerk/clerk-expo expo-web-browser expo-auth-session expo-secure-store expo-crypto react-dom react-native-web > /dev/null 2>&1; then
    echo "✅ All dependencies installed successfully!"
else
    echo "⚠️  Some dependencies may not be installed correctly"
    echo "Run 'npm list @clerk/clerk-expo' to check"
fi

echo ""
echo "=========================================="
echo "Next Steps:"
echo "=========================================="
echo ""
echo "1. Create .env file (if not exists):"
echo "   cp .env.example .env"
echo ""
echo "2. Add your Clerk key to .env:"
echo "   EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_your_key_here"
echo ""
echo "3. Clear cache and start:"
echo "   npx expo start -c"
echo ""
echo "📖 See CLERK_SETUP.md for detailed setup instructions"
echo ""
