#!/usr/bin/env bash
# Run the Hawk FastAPI server locally with live reload, from anywhere.
#
# Usage:
#   scripts/dev/api                    # uses hawk/.env, serves on :8080
#   scripts/dev/api --port 9000        # extra args are passed to `fastapi dev`
#   HAWK_ENV_FILE=hawk/.env.mish1 scripts/dev/api
#
# Loads environment from hawk/.env (override with HAWK_ENV_FILE) via uv's dotenv
# parser and runs the server in the app's own project env. We use uv --env-file
# rather than `source` because generated values can contain spaces (e.g. the
# OAuth scopes), which shell word-splitting would mangle.

set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
ENV_FILE="${HAWK_ENV_FILE:-${REPO_ROOT}/hawk/.env}"

env_args=()
if [[ -f "${ENV_FILE}" ]]; then
  env_args=(--env-file "${ENV_FILE}")
else
  echo "warning: ${ENV_FILE} not found; starting without it." >&2
  echo "         Generate one with:" >&2
  echo "         (cd ${REPO_ROOT} && uv run python scripts/dev/generate-env.py <stack> --api > hawk/.env)" >&2
fi

exec uv run --directory "${REPO_ROOT}/hawk" --extra api \
  ${env_args[@]+"${env_args[@]}"} \
  fastapi dev "${REPO_ROOT}/hawk/hawk/api/server.py" --host 127.0.0.1 --port 8080 "$@"
