#!/bin/bash

USERNAME="node"
CURRENT_UID=$(id -u ${USERNAME} 2>/dev/null || echo "")
CURRENT_GID=$(id -g ${USERNAME} 2>/dev/null || echo "")

CREDS_DIR="/credentials"
mkdir -p "${CREDS_DIR}/ssh"
mkdir -p "${CREDS_DIR}/git"
mkdir -p "${CREDS_DIR}/gh"
mkdir -p "${CREDS_DIR}/claude-config"


# Set up symlinks for SSH directory
if [ ! -L /home/${USERNAME}/.ssh ] || [ ! -e /home/${USERNAME}/.ssh ]; then
    rm -rf /home/${USERNAME}/.ssh
    ln -sf "${CREDS_DIR}/ssh" /home/${USERNAME}/.ssh
    echo "✅ SSH keys will be stored in workspace"
fi

# Set up Git configuration symlink
if [ ! -L /home/${USERNAME}/.gitconfig ]; then
    if [ -f /home/${USERNAME}/.gitconfig ]; then
        mv /home/${USERNAME}/.gitconfig "${CREDS_DIR}/git/gitconfig" 2>/dev/null || true
    fi
    ln -sf "${CREDS_DIR}/git/gitconfig" /home/${USERNAME}/.gitconfig
    echo "✅ Git config will be stored in workspace"
fi

# Set up GitHub CLI configuration symlink
if [ ! -L /home/${USERNAME}/.config/gh ]; then
    mkdir -p /home/${USERNAME}/.config
    if [ -d /home/${USERNAME}/.config/gh ]; then
        mv /home/${USERNAME}/.config/gh/* "${CREDS_DIR}/gh/" 2>/dev/null || true
        rm -rf /home/${USERNAME}/.config/gh
    fi
    ln -sf "${CREDS_DIR}/gh" /home/${USERNAME}/.config/gh
    echo "✅ GitHub CLI config will be stored in workspace"
fi

# Set up .claude directory symlink (for CLAUDE_CONFIG_DIR)
if [ ! -L /home/${USERNAME}/.claude ]; then
    if [ -d /home/${USERNAME}/.claude ]; then
        mv /home/${USERNAME}/.claude/* "${CREDS_DIR}/claude-config/" 2>/dev/null || true
        rm -rf /home/${USERNAME}/.claude
    fi
    ln -sf "${CREDS_DIR}/claude-config" /home/${USERNAME}/.claude
    echo "✅ Claude config directory will be stored in workspace"
fi

# Set up claude.json symlink
if [ ! -L /home/${USERNAME}/.claude.json ]; then
    # Create claude.json if it doesn't exist
    if [ ! -f "${CREDS_DIR}/claude.json" ]; then
        echo "{}" > "${CREDS_DIR}/claude.json"
        chown ${CURRENT_UID}:${CURRENT_GID} "${CREDS_DIR}/claude.json"
    fi
    
    # Move existing claude.json if present
    if [ -f /home/${USERNAME}/.claude.json ]; then
        mv /home/${USERNAME}/.claude.json "${CREDS_DIR}/claude.json" 2>/dev/null || true
    fi
    
    # Create symlink
    ln -sf "${CREDS_DIR}/claude.json" /home/${USERNAME}/.claude.json
    echo "✅ Claude.json will be stored in workspace"
fi

# Ensure proper permissions on credential directories
chmod 700 "${CREDS_DIR}/ssh"
chmod 700 "${CREDS_DIR}/git"
chmod 700 "${CREDS_DIR}/gh"
chmod 700 "${CREDS_DIR}/claude-config"
chmod 700 "${CREDS_DIR}/claude.json"

if [ ! -z "${GIT_NAME}" ] || [ ! -z "${GIT_EMAIL}" ]; then
    echo "🔧 Configuring Git..."
    
    # Create initial gitconfig if it doesn't exist
    if [ ! -f "${CREDS_DIR}/git/gitconfig" ]; then
        touch "${CREDS_DIR}/git/gitconfig"
        chown ${USER_ID}:${GROUP_ID} "${CREDS_DIR}/git/gitconfig"
    fi
    
    # Set git name if provided
    if [ ! -z "${GIT_NAME}" ]; then
        su - ${USERNAME} -c "git config --global user.name \"${GIT_NAME}\""
        echo "   • Git user.name set to: ${GIT_NAME}"
    fi
    
    # Set git email if provided
    if [ ! -z "${GIT_EMAIL}" ]; then
        su - ${USERNAME} -c "git config --global user.email \"${GIT_EMAIL}\""
        echo "   • Git user.email set to: ${GIT_EMAIL}"
    fi
    
    echo "✅ Git configuration complete"
fi