#!/usr/bin/env bash
set -euo pipefail

usage() {
  cat >&2 <<'EOF'
Usage:
  semantic_index/refresh.sh [--apply] [--dry-run]

Examples:
  semantic_index/refresh.sh
  semantic_index/refresh.sh --apply

Environment:
  SEMANTIC_INDEX_PROJECT_LIMITS  comma-separated project=limit pairs
  SEMANTIC_INDEX_LOG_DIR         default: .cache/semantic_index/logs
  SEMANTIC_INDEX_STATE_PATH      default: .cache/semantic_index/refresh_state.json
  SEMANTIC_INDEX_OVERLAP_MINUTES default: 15
  PYTHON                         default: <install-root>/.venv/bin/python

This wrapper never passes --force-rebuild. Run force rebuilds manually.
EOF
}

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
install_root=$(cd "$script_dir/.." && pwd)

load_env_defaults() {
  local file=$1
  local key value
  [[ -r "$file" ]] || return 0
  while IFS= read -r line || [[ -n "$line" ]]; do
    line=${line#"${line%%[![:space:]]*}"}
    line=${line%"${line##*[![:space:]]}"}
    [[ -z "$line" || "$line" == \#* || "$line" != *=* ]] && continue
    key=${line%%=*}
    value=${line#*=}
    key=${key%"${key##*[![:space:]]}"}
    value=${value#"${value%%[![:space:]]*}"}
    value=${value%"${value##*[![:space:]]}"}
    value=${value%\"}
    value=${value#\"}
    value=${value%\'}
    value=${value#\'}
    if [[ -z "${!key+x}" ]]; then
      export "$key=$value"
    fi
  done < "$file"
}

load_env_defaults /etc/semantic-index.env

mode=dry-run
while [[ $# -gt 0 ]]; do
  case "$1" in
    --apply)
      mode=apply
      shift
      ;;
    --dry-run)
      mode=dry-run
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      usage
      exit 2
      ;;
  esac
done

project_limits=${SEMANTIC_INDEX_PROJECT_LIMITS:-customer-service=500,hiring=200,todo-jason=200,sales-inbox=100,business-development=100,dock-scheduling=100,prep-standardization=100}
log_dir=${SEMANTIC_INDEX_LOG_DIR:-.cache/semantic_index/logs}
state_path=${SEMANTIC_INDEX_STATE_PATH:-.cache/semantic_index/refresh_state.json}
overlap_minutes=${SEMANTIC_INDEX_OVERLAP_MINUTES:-15}
python_bin=${PYTHON:-$install_root/.venv/bin/python}

mkdir -p "$log_dir" "$(dirname "$state_path")"
timestamp=$(date -u +"%Y%m%dT%H%M%SZ")
log_file="$log_dir/redmine-refresh-$timestamp.log"

args=(
  -m semantic_index
  --refresh-redmine-projects
  --project-limits "$project_limits"
  --state-path "$state_path"
  --overlap-minutes "$overlap_minutes"
)

if [[ "$mode" == "dry-run" ]]; then
  args+=(--dry-run)
fi

{
  printf 'started_at=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
  printf 'mode=%s\n' "$mode"
  printf 'project_limits=%s\n' "$project_limits"
  printf 'state_path=%s\n' "$state_path"
  printf 'overlap_minutes=%s\n' "$overlap_minutes"
  cd "$install_root"
  "$python_bin" "${args[@]}"
  printf '\nfinished_at=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
} 2>&1 | tee "$log_file"

printf 'log_file=%s\n' "$log_file"
