#!/usr/bin/env bash
# ReadySet Shallow Cache — Docker Deployment Script
# Generated by RDST. Do not edit {placeholder} values manually.
set -euo pipefail

CONTAINER_NAME="{container_name}"
DB_ENGINE="{db_engine}"
DB_HOST="{db_host}"
DB_PORT="{db_port}"
DB_USER="{db_user}"
DB_NAME="{db_name}"
READYSET_PORT="{readyset_port}"
IMAGE="{readyset_image}"

# ---------------------------------------------------------------------------
# Subcommand handling
# ---------------------------------------------------------------------------
case "${1:-}" in
  stop)
    echo "Stopping ReadySet..."
    docker stop "$CONTAINER_NAME" 2>/dev/null && docker rm "$CONTAINER_NAME" 2>/dev/null
    echo "ReadySet stopped and removed."
    exit 0
    ;;
  logs)
    docker logs -f "$CONTAINER_NAME"
    exit 0
    ;;
  status)
    if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^${CONTAINER_NAME}$"; then
      echo "ReadySet is running:"
      docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
    else
      echo "ReadySet is not running."
      exit 1
    fi
    exit 0
    ;;
  restart)
    echo "Restarting ReadySet..."
    docker restart "$CONTAINER_NAME"
    echo "ReadySet restarted."
    exit 0
    ;;
  uninstall)
    echo "Uninstalling ReadySet..."
    docker stop "$CONTAINER_NAME" 2>/dev/null || true
    docker rm "$CONTAINER_NAME" 2>/dev/null || true
    echo "ReadySet container removed."
    echo "To also remove the image: docker rmi $IMAGE"
    exit 0
    ;;
esac

# ---------------------------------------------------------------------------
# Prerequisite: Docker
# ---------------------------------------------------------------------------
if ! command -v docker >/dev/null 2>&1; then
  echo "Error: Docker is not installed."
  echo "Install Docker: https://docs.docker.com/get-docker/"
  exit 1
fi

if ! docker info >/dev/null 2>&1; then
  echo "Error: Docker daemon is not running."
  echo "Start Docker Desktop or run: sudo systemctl start docker"
  exit 1
fi

# ---------------------------------------------------------------------------
# Idempotency: check if already running
# ---------------------------------------------------------------------------
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^${CONTAINER_NAME}$"; then
  echo "ReadySet is already running: $CONTAINER_NAME"
  docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
  echo ""
  echo "Commands:"
  echo "  $0 logs      View logs"
  echo "  $0 status    Check status"
  echo "  $0 restart   Restart"
  echo "  $0 stop      Stop and remove"
  exit 0
fi

# Remove stopped container with same name if exists
if docker ps -a --format '{{.Names}}' 2>/dev/null | grep -q "^${CONTAINER_NAME}$"; then
  echo "Removing stopped container..."
  docker rm "$CONTAINER_NAME" >/dev/null 2>&1
fi

# ---------------------------------------------------------------------------
# Password prompt
# ---------------------------------------------------------------------------
read -s -p "Enter database password for user '${DB_USER}': " DB_PASSWORD
echo ""

if [ -z "$DB_PASSWORD" ]; then
  echo "Error: Password cannot be empty."
  exit 1
fi

# ---------------------------------------------------------------------------
# Build connection URL
# ---------------------------------------------------------------------------
if [ "$DB_ENGINE" = "mysql" ]; then
  DB_URL="mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
  DB_TYPE="mysql"
else
  DB_URL="postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
  DB_TYPE="postgresql"
fi

# ---------------------------------------------------------------------------
# Pull and run
# ---------------------------------------------------------------------------
echo ""
echo "Pulling ReadySet image: $IMAGE"
echo "(This may take a few minutes on first run)"
docker pull "$IMAGE"

echo ""
echo "Starting ReadySet container..."
docker run -d \
  --restart=unless-stopped \
  --name "$CONTAINER_NAME" \
  -e UPSTREAM_DB_URL="$DB_URL" \
  -e DATABASE_TYPE="$DB_TYPE" \
  -e LISTEN_ADDRESS="0.0.0.0:${READYSET_PORT}" \
  -e DEPLOYMENT_MODE="standalone" \
  -e QUERY_CACHING="explicit" \
  -e CACHE_MODE="shallow" \
  -p "${READYSET_PORT}:${READYSET_PORT}" \
  "$IMAGE"

echo ""
echo "ReadySet deployed successfully!"
echo ""
echo "  Container: $CONTAINER_NAME"
echo "  Port:      $READYSET_PORT"
echo "  Engine:    $DB_TYPE"
echo ""
echo "Commands:"
echo "  $0 logs      View logs"
echo "  $0 status    Check status"
echo "  $0 restart   Restart"
echo "  $0 stop      Stop and remove"
