# =============================================================================
# Coba 後端服務編排設定檔
# =============================================================================
# 本檔定義五個服務,執行 `docker compose up -d` 後會全部啟動:
#   1. postgres  ─ 主資料庫(存使用者、任務、訊息、會議等所有結構化資料)
#   2. redis     ─ 快取 + WebSocket 訊息分發 + Celery 任務佇列(階段 4 用)
#   3. qdrant    ─ 向量資料庫(階段 3 庫柏 RAG 用,先架好)
#   4. api       ─ FastAPI 後端服務(本專案核心)
#   5. adminer   ─ 資料庫視覺化管理介面(瀏覽器看 PostgreSQL 內容)
#
# 啟動方式(在本檔所在目錄):
#   1. 複製 .env.example 為 .env(第一次)
#   2. docker compose up -d --build
#   3. 開 http://localhost:8000/docs   ← API 文件(Swagger)
#   4. 開 http://localhost:8080         ← 資料庫管理介面(Adminer)
# =============================================================================

services:
  # ---------------------------------------------------------------------------
  # PostgreSQL:主資料庫
  # ---------------------------------------------------------------------------
  postgres:
    image: postgres:16-alpine
    container_name: coba-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      # 對外開 5432 方便我們從本機用 DBeaver / TablePlus 直接連線
      - "5432:5432"
    volumes:
      # 資料持久化:容器砍掉重建,資料還在
      - coba_postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 10

  # ---------------------------------------------------------------------------
  # Redis:快取 + Pub/Sub(WebSocket 訊息廣播)+ Celery broker
  # ---------------------------------------------------------------------------
  redis:
    image: redis:7-alpine
    container_name: coba-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - coba_redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5

  # ---------------------------------------------------------------------------
  # Qdrant:向量資料庫
  # 階段 3 庫柏的 RAG(檢索增強生成)會大量使用,先架好不用之後重建
  # ---------------------------------------------------------------------------
  qdrant:
    image: qdrant/qdrant:v1.12.1
    container_name: coba-qdrant
    restart: unless-stopped
    ports:
      - "6333:6333"   # HTTP API
      - "6334:6334"   # gRPC API
    volumes:
      - coba_qdrant_data:/qdrant/storage

  # ---------------------------------------------------------------------------
  # FastAPI 後端
  # ---------------------------------------------------------------------------
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: coba-api
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      # 從 .env 讀取所有設定值
      DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
      REDIS_URL: redis://redis:6379/0
      QDRANT_URL: http://qdrant:6333
      JWT_SECRET_KEY: ${JWT_SECRET_KEY}
      JWT_ALGORITHM: ${JWT_ALGORITHM}
      ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES}
      REFRESH_TOKEN_EXPIRE_DAYS: ${REFRESH_TOKEN_EXPIRE_DAYS}
      SEED_OWNER_EMAIL: ${SEED_OWNER_EMAIL}
      SEED_OWNER_PASSWORD: ${SEED_OWNER_PASSWORD}
      SEED_OWNER_DISPLAY_NAME: ${SEED_OWNER_DISPLAY_NAME}
      ENVIRONMENT: ${ENVIRONMENT}
    ports:
      - "8000:8000"
    volumes:
      # 開發階段把 app 資料夾掛進容器,改 code 不用重 build
      - ./app:/code/app
      - ./alembic:/code/alembic
      - ./alembic.ini:/code/alembic.ini

  # ---------------------------------------------------------------------------
  # Adminer:資料庫視覺化管理(瀏覽器版的 PgAdmin 簡化版)
  # 給非工程師用,可以直接點開看資料、改資料
  # ---------------------------------------------------------------------------
  adminer:
    image: adminer:4.8.1
    container_name: coba-adminer
    restart: unless-stopped
    depends_on:
      - postgres
    ports:
      - "8080:8080"
    environment:
      ADMINER_DEFAULT_SERVER: postgres
      ADMINER_DESIGN: dracula

# =============================================================================
# 資料卷(Docker named volumes)─ 跟容器分離,容器砍了資料還在
# =============================================================================
volumes:
  coba_postgres_data:
  coba_redis_data:
  coba_qdrant_data:
