#!/usr/bin/env bash
set -e

echo "Runtime: $MUX_RUNTIME"
echo "Project path: $MUX_PROJECT_PATH"
echo "Workspace: $PWD"

if [ "$MUX_RUNTIME" = "ssh" ]; then
  echo "SSH runtime not supported"
  exit 1
fi

if [ "$MUX_RUNTIME" != "local" ]; then
  # Copy .envrc from project root
  if [ -f "../.envrc" ]; then
    echo "Copying .envrc from parent..."
    cp "../.envrc" ".envrc"
  elif [ -n "$MUX_PROJECT_PATH" ] && [ -f "$MUX_PROJECT_PATH/.envrc" ]; then
    echo "Copying .envrc from project root..."
    cp "$MUX_PROJECT_PATH/.envrc" ".envrc"
  fi

  # Copy .luarc.json for Lua language server
  if [ -f "../.luarc.json" ]; then
    echo "Copying .luarc.json from parent..."
    cp "../.luarc.json" ".luarc.json"
  elif [ -n "$MUX_PROJECT_PATH" ] && [ -f "$MUX_PROJECT_PATH/.luarc.json" ]; then
    echo "Copying .luarc.json from project root..."
    cp "$MUX_PROJECT_PATH/.luarc.json" ".luarc.json"
  fi
else
  echo "Local mode: using existing config files"
fi

echo "Setting up Python environment..."
uv sync --group dev --group test

if [ -f ".envrc" ]; then
  echo "Sourcing .envrc..."
  source .envrc
fi

echo "Init complete!"
