version: "3.9"

services:
  postgres:
    image: pgvector/pgvector:pg16
    environment:
      POSTGRES_DB: scilit
      POSTGRES_USER: scilit
      POSTGRES_PASSWORD: scilit_dev
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U scilit"]
      interval: 5s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  elasticsearch:
    image: elasticsearch:8.11.0
    environment:
      discovery.type: single-node
      xpack.security.enabled: "false"
      "ES_JAVA_OPTS": "-Xms512m -Xmx512m"
    ports:
      - "9200:9200"
    volumes:
      - esdata:/usr/share/elasticsearch/data
    healthcheck:
      test: ["CMD-SHELL", "curl -s http://localhost:9200/_cluster/health | grep -q green"]
      interval: 10s
      timeout: 5s
      retries: 5

  minio:
    image: minio/minio
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - miniodata:/data

  migrate:
    build: ./backend
    command: alembic -c alembic/alembic.ini upgrade head
    volumes:
      - ./backend:/app
    depends_on:
      postgres:
        condition: service_healthy

  backend:
    build: ./backend
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload --log-level debug
    volumes:
      - ./backend:/app
    ports:
      - "8000:8000"
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:8000/health || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    environment:
      DATABASE_URL: postgresql+asyncpg://scilit:scilit_dev@postgres:5432/scilit
      REDIS_URL: redis://redis:6379
      ES_URL: http://elasticsearch:9200
      S3_ENDPOINT: http://minio:9000
      S3_ACCESS_KEY: minioadmin
      S3_SECRET_KEY: minioadmin
      S3_BUCKET: scilit-files
      COS_SECRET_ID: ${COS_SECRET_ID:-}
      COS_SECRET_KEY: ${COS_SECRET_KEY:-}
      COS_REGION: ${COS_REGION:-ap-guangzhou}
      COS_BUCKET: ${COS_BUCKET:-scilit-files}
      JWT_SECRET: dev-secret-change-in-production
      SPECIALTY: oncology

  worker:
    build: ./backend
    command: arq app.tasks.worker.WorkerSettings
    volumes:
      - ./backend:/app
    environment:
      DATABASE_URL: postgresql+asyncpg://scilit:scilit_dev@postgres:5432/scilit
      REDIS_URL: redis://redis:6379
      ES_URL: http://elasticsearch:9200
      S3_ENDPOINT: http://minio:9000
      S3_ACCESS_KEY: minioadmin
      S3_SECRET_KEY: minioadmin
      S3_BUCKET: scilit-files
      COS_SECRET_ID: ${COS_SECRET_ID:-}
      COS_SECRET_KEY: ${COS_SECRET_KEY:-}
      COS_REGION: ${COS_REGION:-ap-guangzhou}
      COS_BUCKET: ${COS_BUCKET:-scilit-files}
      JWT_SECRET: dev-secret-change-in-production
      SPECIALTY: oncology
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy

  frontend:
    build: ./frontend
    command: npm run dev -- --host 0.0.0.0
    volumes:
      - ./frontend:/app
      - /app/node_modules
    ports:
      - "5173:5173"
    environment:
      VITE_API_BASE: http://localhost:8000/api/v1
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:5173 || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 30s

volumes:
  pgdata:
  esdata:
  miniodata:
