services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-crank}
      POSTGRES_USER: ${POSTGRES_USER:-crank}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-crank}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "127.0.0.1:5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-crank} -d ${POSTGRES_DB:-crank}"]
      interval: 10s
      timeout: 5s
      retries: 5

  admin-api:
    image: ${CRANK_ADMIN_API_IMAGE:-crank/admin-api:dev}
    build:
      context: .
      dockerfile: apps/admin-api/Dockerfile
    restart: unless-stopped
    environment:
      POSTGRES_HOST: ${POSTGRES_HOST:-postgres}
      POSTGRES_PORT: ${POSTGRES_PORT:-5432}
      POSTGRES_DB: ${POSTGRES_DB:-crank}
      POSTGRES_USER: ${POSTGRES_USER:-crank}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-crank}
      CRANK_STORAGE_ROOT: ${CRANK_STORAGE_ROOT:-/var/lib/crank/storage}
      CRANK_ADMIN_BIND: ${CRANK_ADMIN_BIND:-0.0.0.0:3001}
      CRANK_LOG_LEVEL: ${CRANK_LOG_LEVEL:-info}
      CRANK_MASTER_KEY: ${CRANK_MASTER_KEY}
      CRANK_BASE_URL: ${CRANK_BASE_URL:-http://localhost:3000}
      CRANK_SESSION_SECRET: ${CRANK_SESSION_SECRET}
      CRANK_PASSWORD_PEPPER: ${CRANK_PASSWORD_PEPPER}
      CRANK_SESSION_TTL_HOURS: ${CRANK_SESSION_TTL_HOURS:-24}
      CRANK_BOOTSTRAP_ADMIN_EMAIL: ${CRANK_BOOTSTRAP_ADMIN_EMAIL}
      CRANK_BOOTSTRAP_ADMIN_PASSWORD: ${CRANK_BOOTSTRAP_ADMIN_PASSWORD}
      CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME: ${CRANK_BOOTSTRAP_ADMIN_DISPLAY_NAME:-Crank Owner}
      CRANK_DEMO_SEED: ${CRANK_DEMO_SEED:-false}
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - artifact_storage:${CRANK_STORAGE_ROOT:-/var/lib/crank/storage}
    ports:
      - "${CRANK_PUBLISH_BIND:-127.0.0.1}:3001:3001"
    healthcheck:
      test: ["CMD", "curl", "--fail", "http://127.0.0.1:3001/health"]
      interval: 15s
      timeout: 5s
      retries: 5

  mcp-server:
    image: ${CRANK_MCP_SERVER_IMAGE:-crank/mcp-server:dev}
    build:
      context: .
      dockerfile: apps/mcp-server/Dockerfile
    restart: unless-stopped
    environment:
      POSTGRES_HOST: ${POSTGRES_HOST:-postgres}
      POSTGRES_PORT: ${POSTGRES_PORT:-5432}
      POSTGRES_DB: ${POSTGRES_DB:-crank}
      POSTGRES_USER: ${POSTGRES_USER:-crank}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-crank}
      CRANK_STORAGE_ROOT: ${CRANK_STORAGE_ROOT:-/var/lib/crank/storage}
      CRANK_MCP_BIND: ${CRANK_MCP_BIND:-0.0.0.0:3002}
      CRANK_MCP_REFRESH_MS: ${CRANK_MCP_REFRESH_MS:-5000}
      CRANK_LOG_LEVEL: ${CRANK_LOG_LEVEL:-info}
      CRANK_MASTER_KEY: ${CRANK_MASTER_KEY}
      CRANK_BASE_URL: ${CRANK_BASE_URL:-http://localhost:3000}
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - artifact_storage:${CRANK_STORAGE_ROOT:-/var/lib/crank/storage}
    ports:
      - "${CRANK_PUBLISH_BIND:-127.0.0.1}:3002:3002"
    healthcheck:
      test: ["CMD", "curl", "--fail", "http://127.0.0.1:3002/health"]
      interval: 15s
      timeout: 5s
      retries: 5

  ui:
    image: ${CRANK_UI_IMAGE:-crank/ui:dev}
    build:
      context: .
      dockerfile: apps/ui/Dockerfile
    restart: unless-stopped
    ports:
      - "${CRANK_PUBLISH_BIND:-127.0.0.1}:3000:3000"
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:3000/"]
      interval: 15s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:
  artifact_storage:
