#!/bin/bash

# Script to analyze ACC service repositories and gather migration information
# Usage: ./scripts/analyze-acc-services.sh

BASE_DIR="/Users/iakpabio/Desktop/git-code/tbi-code"
OUTPUT_FILE="docs/ACC-SERVICES-ANALYSIS.md"

echo "=== ACC Services Analysis ==="
echo "Scanning repositories in: ${BASE_DIR}"
echo "Output file: ${OUTPUT_FILE}"
echo ""

# Initialize output file
cat > "${OUTPUT_FILE}" << 'EOF'
# ACC Services Analysis Report

Generated: $(date)

This report contains analysis of all ACC services for CI/CD migration.

## Service Details

EOF

# List of services
services=(
    "tbi-acc-hotel-service"
    "tbi-acc-package-service"
    "tbi-acc-hotel-service-connector"
    "tbi-acc-travelbrandsagent"
    "tbi-acc-profile-service"
    "tbi-acc-booking-service"
    "tbi-acc-quotes-service"
    "tbi-acc-notification-service"
    "tbi-acc-mybooking-service"
    "tbi-acc-mybooking-frontend"
    "tbi-acc-mybff"
    "tbi-acc-ceetiz-connector"
    "tbi-acc-exotiktours-connector"
    "tbi-acc-sabreapi"
    "tbi-acc-profile-sync-service"
    "tbi-acc-travelbrands"
    "tbi-acc-package-cron"
    "tbi-acc-cron-service"
    "tbi-acc-gimmonix-automation"
    "tbi-acc-gimmonix-automation-cron"
    "tbi-acc-gimmonix-automation-cron-2"
    "tbi-acc-gimmonix-automation-cron-3"
    "tbi-acc-gimmonix-automation-cron-4"
    "tbi-acc-gimmonix-automation-cron-5"
    "tbi-acc-gimmonix-automation-cron-6"
    "tbi-acc-clone-mybff"
    "tbi-acc-corporate-booking"
    "tbi-acc-booking-notification"
    "tbi-acc-cronjobs"
)

for service in "${services[@]}"; do
    service_dir="${BASE_DIR}/${service}"
    
    echo "---" >> "${OUTPUT_FILE}"
    echo "" >> "${OUTPUT_FILE}"
    echo "### ${service}" >> "${OUTPUT_FILE}"
    echo "" >> "${OUTPUT_FILE}"
    
    if [ ! -d "${service_dir}" ]; then
        echo "**Status**: ❌ Repository not found" >> "${OUTPUT_FILE}"
        echo "" >> "${OUTPUT_FILE}"
        continue
    fi
    
    echo "**Status**: ✅ Repository exists" >> "${OUTPUT_FILE}"
    echo "" >> "${OUTPUT_FILE}"
    
    cd "${service_dir}"
    
    # Detect technology stack
    echo "**Technology Stack**:" >> "${OUTPUT_FILE}"
    if [ -f "package.json" ]; then
        echo "- Node.js" >> "${OUTPUT_FILE}"
        
        # Check for framework
        if grep -q "\"@nestjs/core\"" package.json 2>/dev/null; then
            echo "- Framework: NestJS" >> "${OUTPUT_FILE}"
        elif grep -q "\"express\"" package.json 2>/dev/null; then
            echo "- Framework: Express" >> "${OUTPUT_FILE}"
        fi
        
        # Get Node version if specified
        if [ -f ".nvmrc" ]; then
            node_version=$(cat .nvmrc)
            echo "- Node Version: ${node_version}" >> "${OUTPUT_FILE}"
        elif grep -q "\"node\":" package.json 2>/dev/null; then
            node_version=$(grep "\"node\":" package.json | head -1)
            echo "- Node Version: ${node_version}" >> "${OUTPUT_FILE}"
        fi
        
        # Get start script
        if grep -q "\"start:prod\"" package.json 2>/dev/null; then
            start_script=$(grep "\"start:prod\"" package.json | sed 's/.*"start:prod": "\([^"]*\)".*/\1/')
            echo "- Start Script: \`${start_script}\`" >> "${OUTPUT_FILE}"
        fi
        
    elif [ -f "requirements.txt" ] || [ -f "pyproject.toml" ]; then
        echo "- Python" >> "${OUTPUT_FILE}"
        
        if grep -q "fastapi" requirements.txt 2>/dev/null || grep -q "fastapi" pyproject.toml 2>/dev/null; then
            echo "- Framework: FastAPI" >> "${OUTPUT_FILE}"
        elif grep -q "flask" requirements.txt 2>/dev/null || grep -q "flask" pyproject.toml 2>/dev/null; then
            echo "- Framework: Flask" >> "${OUTPUT_FILE}"
        elif grep -q "django" requirements.txt 2>/dev/null || grep -q "django" pyproject.toml 2>/dev/null; then
            echo "- Framework: Django" >> "${OUTPUT_FILE}"
        fi
        
    elif [ -f "pom.xml" ] || [ -f "build.gradle" ]; then
        echo "- Java" >> "${OUTPUT_FILE}"
        
        if grep -q "spring-boot" pom.xml 2>/dev/null || grep -q "spring-boot" build.gradle 2>/dev/null; then
            echo "- Framework: Spring Boot" >> "${OUTPUT_FILE}"
        fi
    else
        echo "- Unknown (no package.json, requirements.txt, or build files found)" >> "${OUTPUT_FILE}"
    fi
    echo "" >> "${OUTPUT_FILE}"
    
    # Check for existing CI/CD
    echo "**Existing CI/CD**:" >> "${OUTPUT_FILE}"
    if [ -d ".circleci" ]; then
        echo "- ✅ CircleCI config exists" >> "${OUTPUT_FILE}"
    else
        echo "- ❌ No CircleCI config" >> "${OUTPUT_FILE}"
    fi
    
    if [ -d ".github/workflows" ]; then
        echo "- ✅ GitHub Actions exists" >> "${OUTPUT_FILE}"
    else
        echo "- ❌ No GitHub Actions" >> "${OUTPUT_FILE}"
    fi
    
    if [ -f "Dockerfile" ]; then
        echo "- ✅ Dockerfile exists (in repo root)" >> "${OUTPUT_FILE}"
    else
        echo "- ❌ No Dockerfile in repo root" >> "${OUTPUT_FILE}"
    fi
    echo "" >> "${OUTPUT_FILE}"
    
    # Check for VERSION file
    echo "**Version Management**:" >> "${OUTPUT_FILE}"
    if [ -f "VERSION" ]; then
        version=$(cat VERSION)
        echo "- ✅ VERSION file exists: \`${version}\`" >> "${OUTPUT_FILE}"
    else
        echo "- ❌ No VERSION file" >> "${OUTPUT_FILE}"
    fi
    echo "" >> "${OUTPUT_FILE}"
    
    # Get current branch
    current_branch=$(git branch --show-current 2>/dev/null || echo "unknown")
    echo "**Current Branch**: ${current_branch}" >> "${OUTPUT_FILE}"
    echo "" >> "${OUTPUT_FILE}"
    
    # Check for dependencies
    echo "**Key Dependencies**:" >> "${OUTPUT_FILE}"
    if [ -f "package.json" ]; then
        if grep -q "\"@aws-sdk" package.json 2>/dev/null; then
            echo "- AWS SDK" >> "${OUTPUT_FILE}"
        fi
        if grep -q "\"mysql\"\\|\"mysql2\"\\|\"pg\"\\|\"mongodb\"" package.json 2>/dev/null; then
            echo "- Database client" >> "${OUTPUT_FILE}"
        fi
        if grep -q "\"redis\"" package.json 2>/dev/null; then
            echo "- Redis" >> "${OUTPUT_FILE}"
        fi
    fi
    echo "" >> "${OUTPUT_FILE}"
    
    echo "Processing: ${service}"
done

echo ""
echo "✓ Analysis complete!"
echo "Report saved to: ${OUTPUT_FILE}"
echo ""
echo "Next steps:"
echo "1. Review ${OUTPUT_FILE}"
echo "2. Identify services ready for migration"
echo "3. Start with services that have clear technology stacks"
