#!/usr/bin/env bash
# Dewey environment activation script
# Usage: source ./activate <deploy-name>

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  echo "Error: This script must be sourced, not executed."
  echo "Usage: source ./activate <deploy-name>"
  exit 1
fi

_dewey_script_path() {
  if [[ -n "${ZSH_VERSION:-}" ]]; then
    printf '%s\n' "${(%):-%x}"
  elif [[ -n "${BASH_SOURCE[0]:-}" ]]; then
    printf '%s\n' "${BASH_SOURCE[0]}"
  else
    printf '%s\n' "$0"
  fi
}

_DEWEY_SCRIPT_PATH="$(_dewey_script_path)"
DEWEY_ROOT="$(cd "$(dirname "${_DEWEY_SCRIPT_PATH}")" && pwd)"
unset -f _dewey_script_path
unset _DEWEY_SCRIPT_PATH

_GREEN='\033[0;32m'
_YELLOW='\033[1;33m'
_BLUE='\033[0;34m'
_CYAN='\033[0;36m'
_NC='\033[0m'
_DEWEY_PYTHON=""
_DEWEY_CONDA_ENV_BASE="DEWEY"
_DEWEY_LEGACY_REBIND_VARS=(
  USE_LOCAL_DAYLILY_TAPDB
  USE_LOCAL_DAYLILY_COGNITO
  USE_LOCAL_CLI_CORE_YO
)

_dewey_require_python_import() {
  local module_name="$1"
  local package_name="$2"
  if ! "${_DEWEY_PYTHON:-python}" - "$module_name" <<'PY' >/dev/null 2>&1
import importlib
import sys

importlib.import_module(sys.argv[1])
PY
  then
    _dewey_fail "Missing packaged dependency: ${package_name}"
    return 1
  fi
}

_dewey_sanitize_deployment_code() {
  local raw_value="$1"
  local sanitized=""
  sanitized="$(printf '%s' "$raw_value" | LC_ALL=C sed -E 's/[^A-Za-z0-9-]+/-/g; s/^-+//; s/-+$//')"
  if [[ -z "$sanitized" ]]; then
    sanitized="local"
  fi
  printf '%s\n' "$sanitized"
}

_dewey_validate_deploy_name() {
  local deploy_name="$1"
  if [[ ! "$deploy_name" =~ ^[A-Za-z0-9-]{2,8}$ ]]; then
    echo -e "  ${_YELLOW}⚠${_NC} deploy-name must match ^[A-Za-z0-9-]{2,8}$" >&2
    return 1
  fi
}

if [[ "$#" -ne 1 ]]; then
  echo "Error: Dewey activation requires exactly one positional deploy-name."
  echo "Usage: source ./activate <deploy-name>"
  return 1
fi

_dewey_deployment_code="$1"
if ! _dewey_validate_deploy_name "${_dewey_deployment_code}"; then
  return 1
fi
export DEWEY_DEPLOYMENT_CODE="${_dewey_deployment_code}"
export DEPLOYMENT_CODE="${_dewey_deployment_code}"
export LSMC_DEPLOYMENT_CODE="${_dewey_deployment_code}"
_DEWEY_CONDA_ENV_NAME="${_DEWEY_CONDA_ENV_BASE}-${_dewey_deployment_code}"
unset _dewey_deployment_code

_dewey_fail() {
  echo -e "  ${_YELLOW}⚠${_NC} $1" >&2
  return 1
}

_dewey_reject_legacy_local_rebind_env() {
  local var_name=""
  local var_value=""
  for var_name in "${_DEWEY_LEGACY_REBIND_VARS[@]}"; do
    if [[ -n "${ZSH_VERSION:-}" ]]; then
      eval 'var_value="${(P)var_name-}"'
    else
      eval 'var_value="${!var_name-}"'
    fi
    if [[ -n "$var_value" ]]; then
      _dewey_fail "${var_name} is no longer supported; use packaged dependencies from ${_DEWEY_CONDA_ENV_NAME}"
      return 1
    fi
  done
}

if ! _dewey_reject_legacy_local_rebind_env; then
  return 1
fi

_dewey_distribution_is_editable_from_repo() {
  local repo_root="$1"
  local editable_project_location=""
  local pip_show_output=""
  if [[ -z "$repo_root" ]]; then
    return 1
  fi
  repo_root="$(cd "$repo_root" 2>/dev/null && pwd)"
  if [[ -z "$repo_root" ]]; then
    return 1
  fi
  if ! pip_show_output="$("${_DEWEY_PYTHON:-python}" -m pip show dewey-service 2>/dev/null)"; then
    return 1
  fi
  editable_project_location="$(printf '%s\n' "$pip_show_output" | awk -F': ' '$1 == "Editable project location" { print $2; exit }')"
  if [[ -z "$editable_project_location" ]]; then
    return 1
  fi
  editable_project_location="$(cd "$editable_project_location" 2>/dev/null && pwd)"
  if [[ -z "$editable_project_location" ]]; then
    return 1
  fi
  if [[ "$editable_project_location" != "$repo_root" ]]; then
    return 1
  fi
  return 0
}
_dewey_install_main_repo() {
  echo -e "  ${_CYAN}→${_NC} Installing local Dewey checkout..."
  if ! "${_DEWEY_PYTHON:-python}" -m pip install --no-deps -e "${DEWEY_ROOT}" -q; then
    _dewey_fail "Failed to install local Dewey checkout from ${DEWEY_ROOT}"
    return 1
  fi
  if ! _dewey_distribution_is_editable_from_repo "$DEWEY_ROOT"; then
    _dewey_fail "Dewey is not installed editable from ${DEWEY_ROOT}"
    return 1
  fi
  echo -e "  ${_GREEN}✓${_NC} Using local Dewey checkout: ${DEWEY_ROOT}"
}

_dewey_prepare_tapdb_config_path() {
  local client_id="$1"
  local namespace="$2"
  local xdg_config_home="${XDG_CONFIG_HOME:-$HOME/.config}"
  local deployment_code="${DEWEY_DEPLOYMENT_CODE:-${DEPLOYMENT_CODE:-${LSMC_DEPLOYMENT_CODE:-local}}}"
  local scoped_namespace=""
  deployment_code="$(_dewey_sanitize_deployment_code "${deployment_code}")"
  scoped_namespace="${namespace}-${deployment_code}"
  local user_dir="${xdg_config_home}/tapdb/${client_id}/${scoped_namespace}"
  local user_path="${user_dir}/tapdb-config.yaml"
  local repo_template="$DEWEY_ROOT/config/tapdb-config-${namespace}.yaml"

  if [[ -f "$user_path" ]]; then
    chmod 600 "$user_path" 2>/dev/null || true
    printf '%s\n' "$user_path"
    return 0
  fi

  if [[ ! -f "$repo_template" ]]; then
    printf '%s\n' "$user_path"
    return 0
  fi

  mkdir -p "$user_dir" || return 1
  cp "$repo_template" "$user_path" || return 1
  chmod 600 "$user_path" || return 1
  echo -e "  ${_CYAN}→${_NC} Created secure TapDB config copy: ${user_path}" >&2
  printf '%s\n' "$user_path"
}

_dewey_ensure_editable_repo() {
  local label="$1"
  local module_name="$2"
  local repo_root="$3"
  local extras="${4:-}"
  local install_target=""
  if [[ -z "$repo_root" ]]; then
    return 0
  fi
  if _dewey_distribution_is_editable_from_repo "$repo_root"; then
    echo -e "  ${_GREEN}✓${_NC} Using local ${label} checkout: ${repo_root}"
    return 0
  fi
  install_target="${repo_root}${extras}"
  echo -e "  ${_CYAN}→${_NC} Installing local ${label} checkout..."
  if ! "${_DEWEY_PYTHON:-python}" -m pip install --no-deps -e "$install_target" -q; then
    _dewey_fail "Failed to install local ${label} checkout from ${repo_root}"
    return 1
  fi
  if ! _dewey_distribution_is_editable_from_repo "$repo_root"; then
    _dewey_fail "${label} is not installed editable from ${repo_root}"
    return 1
  fi
  echo -e "  ${_GREEN}✓${_NC} Using local ${label} checkout: ${repo_root}"
}

echo -e "${_BLUE}Activating Dewey environment...${_NC}"

if ! command -v conda &> /dev/null; then
  _dewey_fail "Conda is required but was not found on PATH."
  unset -f _dewey_fail
  return 1
fi

_DEWEY_CONDA_BASE="$(conda info --base 2>/dev/null)"
if [[ -z "${_DEWEY_CONDA_BASE}" ]] || [[ ! -f "${_DEWEY_CONDA_BASE}/etc/profile.d/conda.sh" ]]; then
  _dewey_fail "Unable to locate conda.sh from 'conda info --base'."
  unset _DEWEY_CONDA_BASE
  unset -f _dewey_fail
  return 1
fi

source "${_DEWEY_CONDA_BASE}/etc/profile.d/conda.sh" 2>/dev/null || {
  _dewey_fail "Failed to source conda.sh from ${_DEWEY_CONDA_BASE}."
  unset _DEWEY_CONDA_BASE
  unset -f _dewey_fail
  return 1
}

if [[ "${CONDA_DEFAULT_ENV:-}" == "${_DEWEY_CONDA_ENV_NAME}" ]] || [[ "${CONDA_PREFIX:-}" == "${_DEWEY_CONDA_BASE}/envs/${_DEWEY_CONDA_ENV_NAME}" ]]; then
  echo -e "  ${_GREEN}✓${_NC} Conda environment already active: ${_DEWEY_CONDA_ENV_NAME}"
elif conda info --envs | grep -Eq "(^|[[:space:]])${_DEWEY_CONDA_ENV_NAME}([[:space:]]|$)"; then
  echo -e "  ${_GREEN}✓${_NC} Activating conda environment: ${_DEWEY_CONDA_ENV_NAME}"
  if ! conda activate "${_DEWEY_CONDA_ENV_NAME}"; then
    _dewey_fail "Failed to activate conda environment: ${_DEWEY_CONDA_ENV_NAME}"
    unset _DEWEY_CONDA_BASE
    unset -f _dewey_fail
    return 1
  fi
else
  echo -e "  ${_YELLOW}⚠${_NC} Conda environment '${_DEWEY_CONDA_ENV_NAME}' not found."
  if [[ -f "$DEWEY_ROOT/environment.yaml" ]]; then
    echo -e "  ${_CYAN}→${_NC} Installing conda environment from environment.yaml..."
    if ! conda env create -n "${_DEWEY_CONDA_ENV_NAME}" -f "$DEWEY_ROOT/environment.yaml"; then
      _dewey_fail "Failed to create conda environment from environment.yaml."
      unset _DEWEY_CONDA_BASE
      unset -f _dewey_fail
      return 1
    fi

    echo -e "  ${_GREEN}✓${_NC} Activating conda environment: ${_DEWEY_CONDA_ENV_NAME}"
    if ! conda activate "${_DEWEY_CONDA_ENV_NAME}"; then
      _dewey_fail "Failed to activate conda environment: ${_DEWEY_CONDA_ENV_NAME}"
      unset _DEWEY_CONDA_BASE
      unset -f _dewey_fail
      return 1
    fi
  else
    _dewey_fail "Conda environment '${_DEWEY_CONDA_ENV_NAME}' was not found and environment.yaml is missing."
    unset _DEWEY_CONDA_BASE
    unset -f _dewey_fail
    return 1
  fi
fi

if [[ -z "${CONDA_PREFIX:-}" ]] || [[ ! -d "${CONDA_PREFIX}/bin" ]]; then
  _dewey_fail "Conda environment '${_DEWEY_CONDA_ENV_NAME}' is not active after activation."
  unset _DEWEY_CONDA_BASE
  unset -f _dewey_fail
  return 1
fi

export PATH="${CONDA_PREFIX}/bin:$PATH"

if [[ -n "${_DEWEY_CONDA_BASE:-}" ]] && [[ -d "${_DEWEY_CONDA_BASE}/bin" ]]; then
  export PATH="${_DEWEY_CONDA_BASE}/bin:$PATH"
fi
unset _DEWEY_CONDA_BASE

if [[ -n "${CONDA_PREFIX:-}" ]] && [[ -x "${CONDA_PREFIX}/bin/python" ]]; then
  _DEWEY_PYTHON="${CONDA_PREFIX}/bin/python"
elif command -v python3 &> /dev/null; then
  _DEWEY_PYTHON="$(command -v python3)"
elif command -v python &> /dev/null; then
  _DEWEY_PYTHON="$(command -v python)"
fi

if [[ -n "${_DEWEY_PYTHON}" ]]; then
  _DEWEY_PYTHON_BIN="$(dirname "${_DEWEY_PYTHON}")"
  _DEWEY_SCRIPTS_DIR="$("${_DEWEY_PYTHON}" -c 'import sysconfig; print(sysconfig.get_path("scripts") or "")' 2>/dev/null || true)"
  if [[ -n "${_DEWEY_SCRIPTS_DIR}" ]] && [[ -d "${_DEWEY_SCRIPTS_DIR}" ]]; then
    export PATH="${_DEWEY_SCRIPTS_DIR}:$PATH"
  fi
  export PATH="${_DEWEY_PYTHON_BIN}:$PATH"
fi

if ! _dewey_install_main_repo; then
  return 1
fi

echo -e "  ${_GREEN}✓${_NC} Using packaged daylily-tapdb from ${_DEWEY_CONDA_ENV_NAME}"
echo -e "  ${_GREEN}✓${_NC} Using packaged daylily-cognito from ${_DEWEY_CONDA_ENV_NAME}"
echo -e "  ${_GREEN}✓${_NC} Using packaged cli-core-yo from ${_DEWEY_CONDA_ENV_NAME}"

_dewey_require_python_import "daylily_tapdb" "daylily-tapdb" || return 1
_dewey_require_python_import "daylily_cognito" "daylily-cognito" || return 1
_dewey_require_python_import "cli_core_yo" "cli-core-yo" || return 1

if command -v rehash >/dev/null 2>&1; then
  rehash
elif command -v hash >/dev/null 2>&1; then
  hash -r 2>/dev/null || true
fi

if [[ "$-" == *i* ]]; then
  if [[ -n "${ZSH_VERSION:-}" ]]; then
    if _DEWEY_COMPLETION="$(dewey --show-completion zsh 2>/dev/null)" && eval "${_DEWEY_COMPLETION}" 2>/dev/null; then
      echo -e "  ${_GREEN}✓${_NC} Enabled tab completion for 'dewey' (zsh)"
    fi
    unset _DEWEY_COMPLETION
  elif [[ -n "${BASH_VERSION:-}" ]]; then
    if _DEWEY_COMPLETION="$(dewey --show-completion bash 2>/dev/null)" && eval "${_DEWEY_COMPLETION}" 2>/dev/null; then
      echo -e "  ${_GREEN}✓${_NC} Enabled tab completion for 'dewey' (bash)"
    fi
    unset _DEWEY_COMPLETION
  fi
fi

unset -f _dewey_fail

export DEWEY_ROOT
export DEWEY_ACTIVE=1
export DEWEY_PROJECT_ROOT="$DEWEY_ROOT"
export AWS_PROFILE="${AWS_PROFILE:-lsmc}"
export AWS_REGION="${AWS_REGION:-us-west-2}"
export DATABASE_BACKEND="${DATABASE_BACKEND:-tapdb}"
export DATABASE_TARGET="${DATABASE_TARGET:-local}"
_DEWEY_TAPDB_CLIENT_ID="dewey"
_DEWEY_TAPDB_DATABASE_NAME="dewey"
_DEWEY_TAPDB_ENV="dev"
_DEWEY_USER_TAPDB_CONFIG_PATH="$(_dewey_prepare_tapdb_config_path "$_DEWEY_TAPDB_CLIENT_ID" "$_DEWEY_TAPDB_DATABASE_NAME")" || {
  _dewey_fail "Failed to prepare secure TapDB config path."
  return 1
}
_DEWEY_RUNTIME_TAPDB_CONFIG_PATH="${_DEWEY_USER_TAPDB_CONFIG_PATH}"

if [[ -z "${DEWEY_API_BEARER_TOKEN:-}" ]]; then
  export DEWEY_API_BEARER_TOKEN="dewey-dev-token"
fi

export PATH="$DEWEY_ROOT:$PATH"

if [[ ! -f "$_DEWEY_RUNTIME_TAPDB_CONFIG_PATH" ]]; then
  echo -e "  ${_YELLOW}⚠${_NC} TapDB scoped config not found at ${_DEWEY_RUNTIME_TAPDB_CONFIG_PATH}"
fi

if [[ -f "$DEWEY_ROOT/dewey_deactivate" ]]; then
  dewey_deactivate() {
    source "$DEWEY_ROOT/dewey_deactivate"
  }
fi

echo -e "  ${_GREEN}✓${_NC} AWS_PROFILE=${AWS_PROFILE}"
echo -e "  ${_GREEN}✓${_NC} AWS_REGION=${AWS_REGION}"
echo -e "  ${_GREEN}✓${_NC} Dewey TapDB client=${_DEWEY_TAPDB_CLIENT_ID}"
echo -e "  ${_GREEN}✓${_NC} Dewey TapDB namespace=${_DEWEY_TAPDB_DATABASE_NAME}"
echo -e "  ${_GREEN}✓${_NC} Dewey TapDB env=${_DEWEY_TAPDB_ENV}"
echo -e "  ${_GREEN}✓${_NC} Dewey TapDB config path=${_DEWEY_RUNTIME_TAPDB_CONFIG_PATH}"
echo -e "  ${_GREEN}✓${_NC} DEWEY_ACTIVE=${DEWEY_ACTIVE}"
echo -e "  ${_GREEN}✓${_NC} DEWEY_PROJECT_ROOT=${DEWEY_PROJECT_ROOT}"
echo ""
echo -e "${_CYAN}Dewey environment activated!${_NC}"
echo ""

echo "Commands:"
echo -e "  ${_CYAN}dewey version${_NC}         Show version"
echo -e "  ${_CYAN}dewey info${_NC}            Show runtime info"
echo -e "  ${_CYAN}dewey config${_NC}          path, init, show, validate, edit, reset, status"
echo -e "  ${_CYAN}dewey env${_NC}             status, activate, deactivate, reset"
echo -e "  ${_CYAN}dewey server${_NC}          start, stop, status, logs, restart"
echo -e "  ${_CYAN}dewey db${_NC}              build, seed, reset, nuke"
echo -e "  ${_CYAN}tapdb${_NC}                 shared DB/runtime lifecycle"
echo -e "  ${_CYAN}daycog${_NC}                shared Cognito lifecycle"
echo -e "  ${_CYAN}dewey test${_NC}            run"
echo -e "  ${_CYAN}dewey quality${_NC}         lint, format, check"
echo ""
echo -e "  ${_GREEN}dewey --help${_NC}        Show all commands"
echo -e "  ${_GREEN}dewey_deactivate${_NC}  Deactivate this environment"

unset _DEWEY_CLI_CORE_REPO
unset _DEWEY_DAYCOG_REPO
unset _DEWEY_TAPDB_REPO
unset _DEWEY_USER_TAPDB_CONFIG_PATH
 
