#!/bin/bash

echo "========================================="
echo "  Amadeus API Integration Setup"
echo "========================================="
echo ""

# Check if .env file exists
if [ ! -f .env ]; then
    echo "Creating .env file from .env.example..."
    cp .env.example .env
    echo "✅ .env file created"
else
    echo "✅ .env file already exists"
fi

echo ""
echo "📋 Current Amadeus Configuration:"
echo "-----------------------------------"

# Check current configuration
if grep -q "AMADEUS_API_KEY=your_amadeus_api_key_here" .env || grep -q "AMADEUS_API_KEY=$" .env; then
    echo "❌ API Key: Not configured"
    NEEDS_CONFIG=true
else
    echo "✅ API Key: Configured"
fi

if grep -q "AMADEUS_API_SECRET=your_amadeus_api_secret_here" .env || grep -q "AMADEUS_API_SECRET=$" .env; then
    echo "❌ API Secret: Not configured"
    NEEDS_CONFIG=true
else
    echo "✅ API Secret: Configured"
fi

BASE_URL=$(grep "AMADEUS_API_BASE_URL=" .env | cut -d'=' -f2)
echo "🌐 Base URL: $BASE_URL"

echo ""
echo "========================================="
echo ""

if [ "$NEEDS_CONFIG" = true ]; then
    echo "⚠️  Amadeus API credentials are not configured!"
    echo ""
    echo "To configure Amadeus API:"
    echo ""
    echo "1. Sign up at: https://developers.amadeus.com/"
    echo "2. Create a new app in your dashboard"
    echo "3. Copy your API Key and API Secret"
    echo "4. Edit the .env file and add your credentials:"
    echo ""
    echo "   AMADEUS_API_KEY=your_actual_api_key"
    echo "   AMADEUS_API_SECRET=your_actual_api_secret"
    echo ""
    echo "5. Restart your application:"
    echo "   docker-compose restart app"
    echo ""
    echo "📖 For detailed instructions, see: docs/AMADEUS_INTEGRATION_GUIDE.md"
    echo ""
    
    read -p "Do you want to configure credentials now? (y/n) " -n 1 -r
    echo ""
    
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo ""
        read -p "Enter your Amadeus API Key: " API_KEY
        read -p "Enter your Amadeus API Secret: " API_SECRET
        
        # Update .env file
        sed -i.bak "s|AMADEUS_API_KEY=.*|AMADEUS_API_KEY=$API_KEY|g" .env
        sed -i.bak "s|AMADEUS_API_SECRET=.*|AMADEUS_API_SECRET=$API_SECRET|g" .env
        rm .env.bak 2>/dev/null
        
        echo ""
        echo "✅ Credentials saved to .env file"
        echo ""
        echo "Restarting application..."
        docker-compose restart app
        
        echo ""
        echo "⏳ Waiting for application to start (30 seconds)..."
        sleep 30
        
        echo ""
        echo "Checking Amadeus authentication..."
        docker-compose logs app | grep -i "amadeus" | tail -10
        
        echo ""
        echo "✅ Setup complete!"
    fi
else
    echo "✅ Amadeus API is already configured!"
    echo ""
    echo "Testing authentication..."
    docker-compose logs app | grep -i "amadeus" | tail -5
    echo ""
    echo "To test the integration, run:"
    echo "  ./test-amadeus-integration.sh"
fi

echo ""
echo "========================================="
echo "  Useful Commands"
echo "========================================="
echo ""
echo "View Amadeus logs:"
echo "  docker-compose logs app | grep -i amadeus"
echo ""
echo "Check health status:"
echo "  curl http://localhost:8081/actuator/health | jq '.components.amadeusApi'"
echo ""
echo "Test flight search:"
echo "  ./test-amadeus-integration.sh"
echo ""
echo "Read full guide:"
echo "  cat docs/AMADEUS_INTEGRATION_GUIDE.md"
echo ""
