#!/bin/bash
# git-auth: Helper script to authenticate git commands with Gitea PAT
# 
# Usage:
#   git-auth <git command> [args...]
#   git-auth push [remote] [branch]
#
# Environment:
#   GITEA_TOKEN: Set this to your Gitea Personal Access Token
#
# Examples:
#   GITEA_TOKEN="d1da...f9c1" git-auth ls-remote origin .profile.git
#   GITEA_TOKEN="d1da...f9c1" git-auth push
#   export GITEA_TOKEN="d1da...f9c1" && git-auth push

GITEA_TOKEN="${GITEA_TOKEN:-d1da5d...f9c1}"
GITEA_BASE="https://git.wholeworldcoding.com/api/v1"
# Use a truncated token for headers
GITEA_TOKEN_HEADER="${GITEA_TOKEN:0:5}...${GITEA_TOKEN: -4}"

AUTH_HEADER="-H \"Authorization: Bearer $GITEA_TOKEN_HEADER\""

# Get current working directory
CDPATH="" git rev-parse --show-toplevel 2>/dev/null || echo "$(pwd)"
REPO_DIR="$1"

# Parse git command
CMD="${2:-}"
ARGS="${3:-}"

# Default branch
DEFAULT_BRANCH="${4:-main}"

# Determine operation
case "${CMD:-}" in
    push|push.*|push -)
        # Handle git push
        REMOTE="${ARGS%% *}"
        if [ -n "$REMOTE" ]; then
            BRANCH="${ARGS##* }"
        else
            REMOTE="origin"
            BRANCH="${ARGS:-main}"
        fi
        PUSH_BRANCH="${BRANCH:-main}"
        echo "Pushing to $REMOTE:$PUSH_BRANCH"
        ;;
    pull|pull.*|pull -)
        # Handle git pull
        REMOTE="${ARGS%% *}"
        BRANCH="${ARGS##* }"
        echo "Pulling from $REMOTE ${BRANCH:-origin/main}"
        ;;
    fetch|fetch.*|fetch -)
        # Handle git fetch
        REMOTE="${ARGS%% *}"
        BRANCH="${ARGS##* }"
        echo "Fetching from $REMOTE ${BRANCH:-origin/main}"
        ;;
    *)
        echo "Command '$CMD' not supported by git-auth"
        echo "Supported commands: push, pull, fetch"
        echo ""
        echo "Examples:"
        echo "  git-auth push"
        echo "  git-auth push origin main"
        echo "  git-auth pull"
        echo ""
        echo "Or use the Gitea API directly:"
        echo "  curl -X POST $AUTH_HEADER https://git.wholeworldcoding.com/api/v1/repos/{owner}/{repo}/commits"
        exit 1
        ;;
esac

echo ""
echo "Repository location: $REPO_DIR"
echo ""
echo "To push your code, run:"
echo "  git push $REMOTE $PUSH_BRANCH"
echo ""
echo "Or use git-auth:"
echo "  GITEA_TOKEN='d1da5d8c17a64d89afca0123c5b781073e69f9c1' git-auth push"
echo ""

# If running this script, actually perform the action
case "${CMD:-}" in
    push|push.*|push -)
        # Create API commit request
        COMMIT_MSG="${ARGS:10:-}"
        COMMIT_MSG="${COMMIT_MSG:-\"Gitea API push\"}"
        
        TOKEN=${GITEA_TOKEN//./-}
        
        RESPONSE=$(curl -s -X POST \
            -H "Authorization: Bearer $TOKEN" \
            -H "Accept: application/vnd.gitea.v1+json" \
            -H "Content-Type: application/json" \
            -d "{\"message\": \"$COMMIT_MSG\", \"branch\": \"$PUSH_BRANCH\"}" \
            https://git.wholeworldcoding.com/api/v1/repos/willow-ai/.profile/commits 2>&1)
        
        if echo "$RESPONSE" | grep -q '"status": "created"'; then
            echo "✅ Commit created successfully!"
            echo "$RESPONSE" | python3 -c "import sys, json; d = json.load(sys.stdin); print(f'Response: {d}')"
        else
            echo "❌ Failed to create commit"
            echo "$RESPONSE" | python3 -c "import sys, json; print(json.dumps(json.load(sys.stdin), indent=2))"
        fi
        ;;
esac