#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMPOSE_FILE="$SCRIPT_DIR/docker-compose.dev.yml"

find_compose_cmd() {
  if command -v docker-compose >/dev/null 2>&1; then
    COMPOSE_CMD=(docker-compose)
    return
  fi

  if command -v docker >/dev/null 2>&1; then
    COMPOSE_CMD=(docker compose)
    return
  fi

  echo "Docker Compose not found. Install docker-compose or Docker Desktop first."
  exit 1
}

main() {
  find_compose_cmd

  echo "Stopping containers and removing volumes..."
  "${COMPOSE_CMD[@]}" -f "$COMPOSE_FILE" down -v

  echo "Rebuilding and starting containers..."
  "${COMPOSE_CMD[@]}" -f "$COMPOSE_FILE" up --build
}

main "$@"
