#!/bin/bash

# Script to change version across all Python client files
# Usage: ./run_change_version.sh

set -e

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

echo -e "${BLUE}GoodMem Python Client Version Updater${NC}"
echo "======================================="

# Files that contain version information
VERSION_FILES=(
    "README.md"
    "pyproject.toml"
    "goodmem_client/__init__.py"
    "setup.py"
    "goodmem_client/configuration.py"
    "goodmem_client/api_client.py"
)

# Function to extract version from a file
extract_version() {
    local file="$1"
    case "$file" in
        "README.md")
            grep "Package version:" "$file" | sed 's/.*Package version: \([0-9.]*\).*/\1/'
            ;;
        "pyproject.toml")
            grep "^version =" "$file" | sed 's/version = "\([0-9.]*\)"/\1/'
            ;;
        "goodmem_client/__init__.py")
            grep "__version__ =" "$file" | sed 's/__version__ = "\([0-9.]*\)"/\1/'
            ;;
        "setup.py")
            grep "^VERSION =" "$file" | sed 's/VERSION = "\([0-9.]*\)"/\1/'
            ;;
        "goodmem_client/configuration.py")
            grep "SDK Package Version:" "$file" | sed 's/.*SDK Package Version: \([0-9.]*\)".*/\1/'
            ;;
        "goodmem_client/api_client.py")
            grep "OpenAPI-Generator/" "$file" | sed 's/.*OpenAPI-Generator\/\([0-9.]*\)\/.*/\1/'
            ;;
    esac
}

# Function to update version in a file
update_version() {
    local file="$1"
    local old_version="$2"
    local new_version="$3"
    
    case "$file" in
        "README.md")
            sed -i "s/Package version: $old_version/Package version: $new_version/" "$file"
            ;;
        "pyproject.toml")
            sed -i "s/version = \"$old_version\"/version = \"$new_version\"/" "$file"
            ;;
        "goodmem_client/__init__.py")
            sed -i "s/__version__ = \"$old_version\"/__version__ = \"$new_version\"/" "$file"
            ;;
        "setup.py")
            sed -i "s/VERSION = \"$old_version\"/VERSION = \"$new_version\"/" "$file"
            ;;
        "goodmem_client/configuration.py")
            sed -i "s/SDK Package Version: $old_version\"/SDK Package Version: $new_version\"/" "$file"
            ;;
        "goodmem_client/api_client.py")
            sed -i "s/OpenAPI-Generator\/$old_version\/python/OpenAPI-Generator\/$new_version\/python/" "$file"
            ;;
    esac
}

# Detect current versions
echo -e "\n${YELLOW}Detecting current versions...${NC}"
declare -A file_versions
consistent_version=""
version_consistent=true

for file in "${VERSION_FILES[@]}"; do
    if [[ -f "$file" ]]; then
        version=$(extract_version "$file")
        file_versions["$file"]="$version"
        echo "  $file: $version"
        
        if [[ -z "$consistent_version" ]]; then
            consistent_version="$version"
        elif [[ "$consistent_version" != "$version" ]]; then
            version_consistent=false
        fi
    else
        echo -e "  ${RED}$file: FILE NOT FOUND${NC}"
        exit 1
    fi
done

# Check version consistency
if [[ "$version_consistent" == true ]]; then
    echo -e "\n${GREEN}✓ All files have consistent version: $consistent_version${NC}"
else
    echo -e "\n${RED}⚠ Warning: Version inconsistency detected!${NC}"
    echo "Please fix version inconsistencies before proceeding."
    exit 1
fi

# Ask for new version
echo -e "\n${BLUE}Current version: $consistent_version${NC}"
echo -n "Enter new version (e.g., 1.2.0): "
read -r new_version

# Validate version format (basic semantic versioning)
if [[ ! "$new_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    echo -e "${RED}Error: Invalid version format. Please use semantic versioning (e.g., 1.2.0)${NC}"
    exit 1
fi

# Confirm the change
echo -e "\n${YELLOW}About to update version from $consistent_version to $new_version in the following files:${NC}"
for file in "${VERSION_FILES[@]}"; do
    echo "  - $file"
done

echo -n "Proceed with version update? (y/N): "
read -r confirm

if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
    echo -e "${YELLOW}Version update cancelled.${NC}"
    exit 0
fi

# Update versions
echo -e "\n${YELLOW}Updating versions...${NC}"
for file in "${VERSION_FILES[@]}"; do
    echo "  Updating $file..."
    update_version "$file" "$consistent_version" "$new_version"
done

# Verify the changes
echo -e "\n${YELLOW}Verifying changes...${NC}"
all_updated=true

for file in "${VERSION_FILES[@]}"; do
    updated_version=$(extract_version "$file")
    if [[ "$updated_version" == "$new_version" ]]; then
        echo -e "  ${GREEN}✓ $file: $updated_version${NC}"
    else
        echo -e "  ${RED}✗ $file: $updated_version (expected $new_version)${NC}"
        all_updated=false
    fi
done

if [[ "$all_updated" == true ]]; then
    echo -e "\n${GREEN}✓ Version successfully updated from $consistent_version to $new_version!${NC}"
    echo -e "\n${BLUE}Next steps:${NC}"
    echo "  1. Review the changes: git diff"
    echo "  2. Test the updated client"
    echo "  3. Commit the changes: git add . && git commit -m \"bump version to $new_version\""
    echo "  4. Build and publish: python setup.py sdist bdist_wheel"
else
    echo -e "\n${RED}⚠ Some files were not updated correctly. Please check manually.${NC}"
    exit 1
fi