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

# Full rebuild — builds a new image from source and restarts.
# Use after code changes that rebuild_and_restart can't handle
# (e.g. binary rename, Dockerfile changes, new system deps).
#
# Pass --no-cache for a clean rebuild (slower but guaranteed fresh).

cd "$(dirname "$0")/.."

if [[ -f .env ]]; then
    set -a
    source .env
    set +a
fi

CACHE_FLAG=""
if [[ "${1:-}" == "--no-cache" ]]; then
    CACHE_FLAG="--no-cache"
fi

docker compose -f docker/docker-compose.yml down
docker compose -f docker/docker-compose.yml build $CACHE_FLAG
docker compose -f docker/docker-compose.yml up -d

echo "Rebuild complete. Logs: docker compose -f docker/docker-compose.yml logs -f"
