#!/usr/bin/env bash
# Build a server-ready archive. Excludes local venv, caches, and optional research bulk.
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PROFILE="${1:-daily}"
STAMP="$(date +%Y%m%dT%H%M%S)"
OUT="${ROOT}/dist/psx-signal-engine-${PROFILE}-${STAMP}.tar.gz"

mkdir -p "${ROOT}/dist"

EXCLUDES=(
  --exclude='.venv'
  --exclude='__pycache__'
  --exclude='.pytest_cache'
  --exclude='.coverage'
  --exclude='.DS_Store'
  --exclude='dist'
  --exclude='data/inbox/yahoo/.daily-cache'
  --exclude='data/inbox/pypsx_toolkit/.cache'
)

case "${PROFILE}" in
  daily)
    # Prospective daily ops: normalized datasets, frozen model, study state.
    EXCLUDES+=(
      --exclude='data/features'
      --exclude='data/raw'
      --exclude='reports/task*'
    )
    ;;
  full)
    # Include research artifacts (features, raw snapshots, task reports).
    ;;
  *)
    echo "Usage: $0 [daily|full]" >&2
    exit 1
    ;;
esac

cd "${ROOT}"
tar -czf "${OUT}" "${EXCLUDES[@]}" \
  .env.example \
  config \
  pyproject.toml \
  README.md \
  DEPLOY.md \
  scripts \
  src \
  tests \
  models \
  data \
  reports

BYTES="$(wc -c < "${OUT}" | tr -d ' ')"
MB="$(awk "BEGIN {printf \"%.1f\", ${BYTES}/1024/1024}")"
echo "Created ${OUT} (${MB} MB, profile=${PROFILE})"
echo
echo "Upload this archive to the server, then follow DEPLOY.md."
