#!/usr/bin/env bash

#################################################################################
# Setup
#################################################################################

PORT_FILE="$TMPDIR/mise_git_http_port"
READY_FILE="$TMPDIR/mise_git_http_ready"
INFO_FILE="$TMPDIR/mise_git_http_info"

# Clean up any previous server files
rm -f "$PORT_FILE" "$READY_FILE" "$INFO_FILE"

# Start local git HTTP server with OS-assigned port to avoid race conditions
MISE_GIT_HTTP_PORT_FILE="$PORT_FILE" \
  MISE_GIT_HTTP_READY_FILE="$READY_FILE" \
  MISE_GIT_HTTP_INFO_FILE="$INFO_FILE" \
  python3 "${TEST_ROOT}/helpers/scripts/git_http_backend_server.py" 0 &
SERVER_PID=$!

# Wait for server to be ready (with timeout)
wait_for_server() {
  local max_attempts=30 # 30 seconds timeout
  local attempt=1

  while [ $attempt -le $max_attempts ]; do
    if [ -f "$READY_FILE" ] && [ -f "$PORT_FILE" ]; then
      return 0
    fi
    sleep 1
    attempt=$((attempt + 1))
  done

  echo "ERROR: Git HTTP server failed to start within 30 seconds"
  kill "$SERVER_PID" 2>/dev/null || true
  exit 1
}

wait_for_server

# Read the actual port from the file
SERVER_PORT=$(cat "$PORT_FILE")
LOCAL_GIT_URL="http://localhost:${SERVER_PORT}/repo.git"

# Update cache directory paths for local server
REMOTE_TASKS_DIR="${MISE_CACHE_DIR}/remote-git-tasks-cache"
# Hash for localhost URL will be different than GitHub URL
LOCAL_CACHE_HASH=$(echo -n "${LOCAL_GIT_URL}v2025.1.17" | shasum -a 256 | cut -d' ' -f1)
MISE_LOCAL_CACHE_DIR="${REMOTE_TASKS_DIR}/${LOCAL_CACHE_HASH}"

git init

# Ensure cleanup on exit
cleanup() {
  kill "$SERVER_PID" 2>/dev/null || true
  rm -f "$PORT_FILE" "$READY_FILE" "$INFO_FILE"
}
trap cleanup EXIT

#################################################################################
# Test remote tasks with no ref
#################################################################################

cat <<EOF >mise.toml
[tasks.remote_lint_https_latest]
file  = "git::${LOCAL_GIT_URL}//xtasks/lint/ripgrep"
EOF

assert_contains "mise tasks" "remote_lint_https_latest"
assert_succeed "mise run remote_lint_https_latest" # Remote task should be downloaded
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_LOCAL_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

assert_succeed "MISE_TASK_REMOTE_NO_CACHE=true mise run remote_lint_https_latest" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_LOCAL_CACHE_DIR}"

assert_succeed "mise run --no-cache remote_lint_https_latest" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_LOCAL_CACHE_DIR}"

assert_succeed "mise run remote_lint_https_latest" # Cache should be used
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_LOCAL_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

#################################################################################
# Test remote tasks with with ref
#################################################################################

cat <<EOF >mise.toml
[tasks.remote_lint_https_ref]
file  = "git::${LOCAL_GIT_URL}//xtasks/lint/ripgrep?ref=v2025.1.17"
EOF

assert_contains "mise tasks" "remote_lint_https_ref"
assert_succeed "mise run remote_lint_https_ref" # Remote task should be downloaded
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_exists "${MISE_LOCAL_CACHE_DIR}"
assert_directory_not_empty "${MISE_LOCAL_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

assert_succeed "MISE_TASK_REMOTE_NO_CACHE=true mise run remote_lint_https_ref" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_LOCAL_CACHE_DIR}"

assert_succeed "mise run --no-cache remote_lint_https_ref" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_LOCAL_CACHE_DIR}"

assert_succeed "mise run remote_lint_https_ref" # Cache should be used
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_exists "${MISE_LOCAL_CACHE_DIR}"
assert_directory_not_empty "${MISE_LOCAL_CACHE_DIR}"

#################################################################################
# Test usage and info commands with git remote tasks
# Verifies fix for https://github.com/jdx/mise/discussions/7578
#################################################################################

# Usage should work and show task in completion output
assert_contains "mise tasks ls --usage" 'cmd remote_lint_https_ref'

# Info should show local cached path, not git URL
assert_contains "mise tasks info remote_lint_https_ref" "remote-git-tasks-cache"
assert_not_contains "mise tasks info remote_lint_https_ref" "git::http"

# JSON output should also work
assert_contains "mise tasks info remote_lint_https_ref --json" '"file":'
assert_contains "mise tasks info remote_lint_https_ref --json" "remote-git-tasks-cache"

# Validate should work with remote tasks
assert_succeed "mise tasks validate remote_lint_https_ref"
