#!/bin/bash
set -e

echo "=== TBI ACC Cron Services Migration Script ==="
echo ""

# Configuration
DEVOPS_REPO="$HOME/Desktop/git-code/tbi-code/tbi-devops-environments"
CODE_REPO_BASE="$HOME/Desktop/git-code/tbi-code"

# Service 1: tbi-acc-cron-service
SERVICE1="tbi-acc-cron-service"
SERVICE1_REPO="${CODE_REPO_BASE}/${SERVICE1}"

# Service 2: tbi-acc-package-cron
SERVICE2="tbi-acc-package-cron"
SERVICE2_REPO="${CODE_REPO_BASE}/${SERVICE2}"

# Function to copy files to service repository
copy_files_to_service() {
    local service_name=$1
    local service_repo=$2
    
    echo "=== Copying files for ${service_name} ==="
    
    if [ ! -d "${service_repo}" ]; then
        echo "ERROR: Service repository not found: ${service_repo}"
        return 1
    fi
    
    cd "${service_repo}"
    
    # Create branch
    echo "Creating circleci-migration branch..."
    git checkout -b circleci-migration 2>/dev/null || git checkout circleci-migration
    
    # Copy CircleCI config
    echo "Copying .circleci/..."
    cp -r "${DEVOPS_REPO}/dev/${service_name}/.circleci" .
    
    # Copy Docker files
    echo "Copying docker/..."
    cp -r "${DEVOPS_REPO}/dev/${service_name}/docker" .
    
    # Copy scripts
    echo "Copying scripts/..."
    cp -r "${DEVOPS_REPO}/dev/${service_name}/scripts" .
    chmod +x scripts/*.sh
    
    # Copy charts
    echo "Copying charts/..."
    cp -r "${DEVOPS_REPO}/dev/${service_name}/charts" .
    
    echo "✓ Files copied for ${service_name}"
    echo ""
}

# Function to show git status and commit instructions
show_commit_instructions() {
    local service_name=$1
    local service_repo=$2
    
    echo "=== Git Status for ${service_name} ==="
    cd "${service_repo}"
    git status
    echo ""
    echo "To commit and push:"
    echo "  cd ${service_repo}"
    echo "  git add .circleci/ docker/ scripts/ charts/"
    echo "  git commit -m 'Add CircleCI pipeline configuration'"
    echo "  git push origin circleci-migration"
    echo ""
}

# Main execution
echo "This script will copy CircleCI pipeline files to service repositories."
echo ""
echo "Services to migrate:"
echo "  1. ${SERVICE1} (PHP 8.0 Lumen)"
echo "  2. ${SERVICE2} (PHP 7.3 Oracle)"
echo ""
read -p "Continue? (y/n) " -n 1 -r
echo ""

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Migration cancelled."
    exit 0
fi

# Migrate Service 1
echo ""
echo "=== Migrating ${SERVICE1} ==="
copy_files_to_service "${SERVICE1}" "${SERVICE1_REPO}"
show_commit_instructions "${SERVICE1}" "${SERVICE1_REPO}"

# Migrate Service 2
echo ""
echo "=== Migrating ${SERVICE2} ==="
copy_files_to_service "${SERVICE2}" "${SERVICE2_REPO}"
show_commit_instructions "${SERVICE2}" "${SERVICE2_REPO}"

echo ""
echo "=== Migration Complete ==="
echo ""
echo "Next steps:"
echo "  1. Review changes in each service repository"
echo "  2. Commit and push to circleci-migration branch"
echo "  3. Create values and secrets in tbi-environments repository"
echo "  4. Test deployments"
echo ""
echo "See docs/BATCH-3-CRON-SERVICES-MIGRATION.md for detailed instructions."
