#!/bin/bash

# Pre-commit hook to set or increment version number
# Usage: ./version [version | --commit]
#   If version is provided, sets that version everywhere
#   If --commit is provided, uses git commit count + 1 as the dev number
#   If no argument is provided, increments the dev number (e.g., dev.10 -> dev.11)

BUILD_ZON_FILE="build.zig.zon"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
BUILD_ZON_PATH="$PROJECT_ROOT/$BUILD_ZON_FILE"

# Check if file exists
if [ ! -f "$BUILD_ZON_PATH" ]; then
    echo "Error: $BUILD_ZON_FILE not found at $BUILD_ZON_PATH"
    exit 1
fi

# Extract current version
CURRENT_VERSION=$(grep -E '^\s*\.version\s*=' "$BUILD_ZON_PATH" | sed -E 's/.*"([^"]+)".*/\1/')

if [ -z "$CURRENT_VERSION" ]; then
    echo "Error: Could not find version in $BUILD_ZON_FILE"
    exit 1
fi

# Check if --commit flag was provided
if [ "$1" = "--commit" ]; then
    # Get total git commit count + 1
    COMMIT_COUNT=$(git -C "$PROJECT_ROOT" rev-list --count HEAD 2>/dev/null)
    if [ -z "$COMMIT_COUNT" ]; then
        echo "Error: Could not get git commit count"
        exit 1
    fi
    NEW_DEV_NUMBER=$((COMMIT_COUNT + 1))
    NEW_VERSION=$(echo "$CURRENT_VERSION" | sed -E "s/dev\.[0-9]+/dev.$NEW_DEV_NUMBER/")
# Check if version was provided as argument
elif [ -n "$1" ]; then
    NEW_VERSION="$1"
    echo "Setting version to: $NEW_VERSION"
else
    # Extract the dev number (e.g., extract 10 from "0.1.0-dev.10")
    DEV_NUMBER=$(echo "$CURRENT_VERSION" | grep -oE 'dev\.([0-9]+)' | grep -oE '[0-9]+')

    if [ -z "$DEV_NUMBER" ]; then
        echo "Error: Could not extract dev number from version: $CURRENT_VERSION"
        exit 1
    fi

    # Increment the dev number
    NEW_DEV_NUMBER=$((DEV_NUMBER + 1))

    # Create new version string (replace dev.XX with dev.NEW_NUMBER)
    NEW_VERSION=$(echo "$CURRENT_VERSION" | sed -E "s/dev\.$DEV_NUMBER/dev.$NEW_DEV_NUMBER/")
fi

# Version pattern to match any dev version (e.g., 0.1.0-dev.XXX)
VERSION_PATTERN="0\\.1\\.0-dev\\.[0-9]+"

# Update build.zig.zon (only .version field, not .jsglue_version)
sed -i.bak -E "/^[[:space:]]*\.version[[:space:]]*=/s/$VERSION_PATTERN/$NEW_VERSION/" "$BUILD_ZON_PATH"
rm -f "$BUILD_ZON_PATH.bak"
echo "Version: $CURRENT_VERSION -> $NEW_VERSION"
echo "Updated $BUILD_ZON_FILE"

# Update pkg/ziex/package.json version
ZIEX_PACKAGE_JSON="$PROJECT_ROOT/pkg/ziex/package.json"
if [ -f "$ZIEX_PACKAGE_JSON" ]; then
    sed -i.bak -E "s/$VERSION_PATTERN/$NEW_VERSION/" "$ZIEX_PACKAGE_JSON"
    rm -f "$ZIEX_PACKAGE_JSON.bak"
    echo "Updated pkg/ziex/package.json"
fi

# Update ide/zed/extension.toml version
ZED_EXTENSION_TOML="$PROJECT_ROOT/ide/zed/extension.toml"
if [ -f "$ZED_EXTENSION_TOML" ]; then
    sed -i.bak -E "s/$VERSION_PATTERN/$NEW_VERSION/" "$ZED_EXTENSION_TOML"
    rm -f "$ZED_EXTENSION_TOML.bak"
    echo "Updated ide/zed/extension.toml"
fi

# Update ide/vscode/package.json version (uses 0.1.X format since VSCode doesn't support prerelease versions)
VSCODE_PACKAGE_JSON="$PROJECT_ROOT/ide/vscode/package.json"
if [ -f "$VSCODE_PACKAGE_JSON" ]; then
    # Extract the dev number from the new version (e.g., 357 from 0.1.0-dev.357)
    VSCODE_DEV_NUMBER=$(echo "$NEW_VERSION" | grep -oE 'dev\.([0-9]+)' | grep -oE '[0-9]+')
    if [ -n "$VSCODE_DEV_NUMBER" ]; then
        VSCODE_VERSION="0.1.$VSCODE_DEV_NUMBER"
        sed -i.bak -E "s/\"version\": \"0\\.1\\.[0-9]+\"/\"version\": \"$VSCODE_VERSION\"/" "$VSCODE_PACKAGE_JSON"
        rm -f "$VSCODE_PACKAGE_JSON.bak"
        echo "Updated ide/vscode/package.json -> $VSCODE_VERSION"
    fi
fi

# Stage the updated files for commit
git add "$BUILD_ZON_PATH"
git add "$ZIEX_PACKAGE_JSON" 2>/dev/null || true
git add "$ZED_EXTENSION_TOML" 2>/dev/null || true
git add "$VSCODE_PACKAGE_JSON" 2>/dev/null || true

# Update tree-sitter-zx version (ignore errors)
TREE_SITTER_DIR="$PROJECT_ROOT/pkg/tree-sitter-zx"
if [ -d "$TREE_SITTER_DIR" ]; then
    (cd "$TREE_SITTER_DIR" && tree-sitter version "$NEW_VERSION") 2>/dev/null || true
    git add "$TREE_SITTER_DIR" 2>/dev/null || true
fi

# Update tree-sitter-mdzx version (ignore errors)
TREE_SITTER_DIR="$PROJECT_ROOT/pkg/tree-sitter-mdzx"
if [ -d "$TREE_SITTER_DIR" ]; then
    (cd "$TREE_SITTER_DIR" && tree-sitter version "$NEW_VERSION") 2>/dev/null || true
    git add "$TREE_SITTER_DIR" 2>/dev/null || true
fi

exit 0

