#!/bin/bash
# Environment Setup Script for Royal Enfield Workflow Frontend

echo "=================================================="
echo "Royal Enfield - Frontend Environment Setup"
echo "=================================================="
echo ""

# Function to create .env.example
create_env_example() {
    cat > .env.example << 'EOF'
# API Configuration
# Backend API base URL (with /api/v1)
VITE_API_BASE_URL=http://localhost:5000/api/v1

# Base URL for direct file access (without /api/v1)
VITE_BASE_URL=http://localhost:5000

# Okta Authentication Configuration
VITE_OKTA_DOMAIN=
VITE_OKTA_CLIENT_ID=

# Push Notifications (Web Push / VAPID)
VITE_PUBLIC_VAPID_KEY=
EOF
    echo "✅ Created .env.example"
}

# Function to create .env.local
create_env_local() {
    cat > .env.local << 'EOF'
# Local Development Environment
VITE_API_BASE_URL=http://localhost:5000/api/v1
VITE_BASE_URL=http://localhost:5000

# Okta Authentication Configuration
VITE_OKTA_DOMAIN=
VITE_OKTA_CLIENT_ID=

# Push Notifications (Web Push / VAPID)
VITE_PUBLIC_VAPID_KEY=
EOF
    echo "✅ Created .env.local (for local development)"
}

# Function to create .env.production template
create_env_production() {
    echo ""
    echo "=================================================="
    echo "Production Environment Configuration"
    echo "=================================================="
    echo ""
    read -p "Enter your PRODUCTION backend URL (e.g., https://api.yourcompany.com): " BACKEND_URL
    read -p "Enter your Okta Domain (e.g., https://your-org.okta.com): " OKTA_DOMAIN
    read -p "Enter your Okta Client ID: " OKTA_CLIENT_ID
    read -p "Enter your VAPID Public Key (for push notifications, optional): " VAPID_KEY
    
    # Remove trailing slash if present
    if [ ! -z "$BACKEND_URL" ]; then
        BACKEND_URL=${BACKEND_URL%/}
    fi
    if [ ! -z "$OKTA_DOMAIN" ]; then
        OKTA_DOMAIN=${OKTA_DOMAIN%/}
    fi
    
    if [ -z "$BACKEND_URL" ]; then
        echo "⚠️  No backend URL provided. Creating template file..."
        cat > .env.production << 'EOF'
# Production Environment
# IMPORTANT: Update these values with your actual production configuration

# API Configuration
VITE_API_BASE_URL=https://your-backend-url.com/api/v1
VITE_BASE_URL=https://your-backend-url.com

# Okta Authentication Configuration
VITE_OKTA_DOMAIN=
VITE_OKTA_CLIENT_ID=

# Push Notifications (Web Push / VAPID)
VITE_PUBLIC_VAPID_KEY=
EOF
    else
        cat > .env.production << EOF
# Production Environment

# API Configuration
VITE_API_BASE_URL=${BACKEND_URL}/api/v1
VITE_BASE_URL=${BACKEND_URL}

# Okta Authentication Configuration
VITE_OKTA_DOMAIN=${OKTA_DOMAIN}
VITE_OKTA_CLIENT_ID=${OKTA_CLIENT_ID}

# Push Notifications (Web Push / VAPID)
VITE_PUBLIC_VAPID_KEY=${VAPID_KEY}
EOF
        echo "✅ Created .env.production with:"
        echo "   - Backend URL: ${BACKEND_URL}"
        [ ! -z "$OKTA_DOMAIN" ] && echo "   - Okta Domain: ${OKTA_DOMAIN}"
        [ ! -z "$OKTA_CLIENT_ID" ] && echo "   - Okta Client ID: ${OKTA_CLIENT_ID}"
        [ ! -z "$VAPID_KEY" ] && echo "   - VAPID Key: Configured"
    fi
}

# Main execution
echo "This script will create environment configuration files for your frontend."
echo ""

# Check if files already exist
if [ -f ".env.local" ] || [ -f ".env.production" ]; then
    echo "⚠️  Environment files already exist:"
    [ -f ".env.local" ] && echo "   - .env.local"
    [ -f ".env.production" ] && echo "   - .env.production"
    echo ""
    read -p "Do you want to OVERWRITE them? (y/n): " -n 1 -r
    echo ""
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Aborted. No files were modified."
        exit 0
    fi
fi

# Create files
create_env_example
create_env_local
create_env_production

echo ""
echo "=================================================="
echo "✅ Setup Complete!"
echo "=================================================="
echo ""
echo "Next Steps:"
echo ""
echo "1. For LOCAL development:"
echo "   npm run dev"
echo "   (will use .env.local automatically)"
echo ""
echo "2. For PRODUCTION deployment:"
echo "   - If deploying to Vercel/Netlify/etc:"
echo "     Set environment variables in your platform dashboard"
echo "   - If using Docker/VM:"
echo "     Ensure .env.production has correct URLs"
echo ""
echo "3. Update Backend CORS:"
echo "   - Add production frontend URL to CORS allowed origins"
echo ""
echo "📖 For detailed instructions, see: DEPLOYMENT_CONFIGURATION.md"
echo ""

