#!/bin/bash
set -e

# Script to clone ACC service repositories for CI/CD migration
# Usage: ./scripts/clone-acc-services.sh

GITHUB_ORG="redlabelvacations"
BASE_DIR="/Users/iakpabio/Desktop/git-code/tbi-code"
CLONE_DIR="${BASE_DIR}"

# Color codes for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo "=== ACC Services Repository Cloner ==="
echo "Target directory: ${CLONE_DIR}"
echo ""

# List of services from Airtable CICD-Pipeline-Migration table
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"
    "tbi-acc-hotel-service-old"
    "tbi-acc-package-service-old"
    "tbi-acc-hotel-service-connector-old"
)

# Skip tbi-acc-hotel-mapping-service as it's already done
echo "Note: Skipping tbi-acc-hotel-mapping-service (already migrated)"
echo ""

# Statistics
total=${#services[@]}
cloned=0
skipped=0
failed=0

for service in "${services[@]}"; do
    echo "----------------------------------------"
    echo "Processing: ${service}"
    
    # Check if directory already exists
    if [ -d "${CLONE_DIR}/${service}" ]; then
        echo -e "${YELLOW}✓ Already exists${NC} - ${CLONE_DIR}/${service}"
        ((skipped++))
        continue
    fi
    
    # Try to clone the repository
    repo_url="https://github.com/${GITHUB_ORG}/${service}.git"
    echo "Cloning from: ${repo_url}"
    
    if git clone "${repo_url}" "${CLONE_DIR}/${service}" 2>/dev/null; then
        echo -e "${GREEN}✓ Cloned successfully${NC}"
        ((cloned++))
    else
        echo -e "${RED}✗ Failed to clone${NC} - Repository may not exist or access denied"
        echo "  You may need to:"
        echo "  - Check if repository exists at: https://github.com/${GITHUB_ORG}/${service}"
        echo "  - Verify GitHub access token/credentials"
        echo "  - Clone manually if repository name differs"
        ((failed++))
    fi
done

echo ""
echo "========================================"
echo "=== Clone Summary ==="
echo "Total services: ${total}"
echo -e "${GREEN}Cloned: ${cloned}${NC}"
echo -e "${YELLOW}Skipped (already exists): ${skipped}${NC}"
echo -e "${RED}Failed: ${failed}${NC}"
echo "========================================"

if [ ${failed} -gt 0 ]; then
    echo ""
    echo "Note: Some repositories failed to clone."
    echo "This could mean:"
    echo "  1. Repository doesn't exist yet"
    echo "  2. Repository has a different name"
    echo "  3. Access permissions issue"
    echo ""
    echo "You can manually clone failed repositories or create them as needed."
fi
