# docker-compose.yml — Local development
# Usage: docker compose up -d
# Test:  curl http://localhost:4000/health
#
# VS Code Web:      docker compose --profile vscode up -d
# Full stack:       docker compose --profile vscode --profile webui up -d
# Dostęp:
#   proxym API:       http://localhost:4000
#   VS Code Web:      http://localhost:8080  (hasło z VSCODE_PASSWORD)
#   Open WebUI:       http://localhost:3000  (profil webui)

# ──────────────────────────────────────────────────────────────
# SIECI
# ──────────────────────────────────────────────────────────────
networks:
  proxym-net:
    driver: bridge

services:
  # ── AI Proxy Server ─────────────────────────────────────────
  proxy:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: proxym
    ports:
      - "${AI_PROXY_PORT:-4000}:4000"
    env_file: .env
    environment:
      - REDIS_URL=redis://redis:6379/0
      - PROXYM_DATA_DIR=/app/data
    depends_on:
      redis:
        condition: service_healthy
    volumes:
      - ./project:/app/project:ro
      - ./.git:/app/.git:ro
      - proxym-data:/app/data
    networks:
      - proxym-net
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-sf", "http://localhost:4000/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s

  proxy-dev:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: proxym-dev
    ports:
      - "${DEV_PORT:-4001}:4000"
    env_file: .env
    environment:
      - REDIS_URL=redis://redis:6379/0
      - AI_PROXY_PORT=4000
      - DASHBOARD_API_URL=http://localhost:${DEV_PORT:-4001}
      - DEFAULT_PROXY_URL=http://localhost:${DEV_PORT:-4001}
      - PYTHONPATH=/app/src
      - HOT_RELOAD=true
      - PROXYM_TOOLS_AVAILABLE=*
      - PROXYM_DATA_DIR=/app/data
    command: ["--host", "0.0.0.0", "--port", "4000", "--reload"]
    depends_on:
      redis:
        condition: service_healthy
    volumes:
      - ./src:/app/src:rw
      - ./frontend:/app/frontend:rw
      - ./project:/app/project:ro
      - ./.git:/app/.git:ro
      - proxym-data:/app/data
    networks:
      - proxym-net
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-sf", "http://localhost:4000/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    profiles:
      - dev

  # ── Redis (cache + semantic search) ─────────────────────────
  redis:
    image: redis/redis-stack:latest
    container_name: proxym-redis
    ports:
      - "6379:6379"
      - "8001:8001"   # RedisInsight UI
    volumes:
      - redis-data:/data
    networks:
      - proxym-net
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    restart: unless-stopped

  # ── VS Code Web — edytor w przeglądarce z Roo Code ─────────
  vscode:
    build:
      context: ./services/vscode
      dockerfile: Dockerfile
    container_name: proxym-vscode
    restart: unless-stopped
    ports:
      - "${VSCODE_PORT:-8080}:8080"
    environment:
      WORKSPACE_NAME: ${VSCODE_WORKSPACE_NAME:-proxym}
      PASSWORD: ${VSCODE_PASSWORD:-proxym-vscode}
      PROXYM_API_BASE: ${PROXYM_API_BASE:-http://proxy:4000/v1}
      PROXYM_API_KEY: ${AI_PROXY_MASTER_KEY:-sk-proxy-local-dev}
      VSCODE_MODEL_CODE: ${VSCODE_MODEL_CODE:-balanced}
      VSCODE_MODEL_ARCHITECT: ${VSCODE_MODEL_ARCHITECT:-free}
      VSCODE_MODEL_DEBUG: ${VSCODE_MODEL_DEBUG:-cheap}
      VSCODE_MODEL_ASK: ${VSCODE_MODEL_ASK:-free}
      VSCODE_MODEL_OPUS: ${VSCODE_MODEL_OPUS:-premium}
    volumes:
      - ${VSCODE_PROJECT_PATH:-.}:/home/coder/project:rw
      - vscode-extensions:/home/coder/.local/share/code-server/extensions
      - vscode-user-data:/home/coder/.local/share/code-server/User
    networks:
      - proxym-net
    depends_on:
      proxy:
        condition: service_healthy
    profiles:
      - vscode

  # ── Open WebUI — chat interface (opcjonalne) ────────────────
  open-webui:
    image: ghcr.io/open-webui/open-webui:main
    container_name: proxym-open-webui
    restart: unless-stopped
    ports:
      - "${OPEN_WEBUI_PORT:-3000}:8080"
    environment:
      OPENAI_API_BASE_URL: http://proxy:4000/v1
      OPENAI_API_KEY: ${AI_PROXY_MASTER_KEY:-sk-proxy-local-dev}
      WEBUI_NAME: proxym Chat
    volumes:
      - open-webui-data:/app/backend/data
    networks:
      - proxym-net
    depends_on:
      proxy:
        condition: service_healthy
    profiles:
      - webui

volumes:
  redis-data:
  proxym-data:
  vscode-extensions:
  vscode-user-data:
  open-webui-data:
