# =============================================================================
# CloudSearch · Docker Compose 部署编排
# =============================================================================
#
# 双模式使用：
#
#   开发/测试模式（从源码构建）：
#     docker compose up -d
#
#   生产模式（使用预构建镜像）：
#     1. 注释掉 services.app.build 段
#     2. 取消注释 services.app.image 行
#     3. docker compose up -d
#
#   构建并推送镜像到私有仓库：
#     ./scripts/build-and-push.sh v0.3.32
#
# =============================================================================

networks:
  cloudsearch-network:
    driver: bridge

volumes:
  app-data:        # 主应用数据（SQLite 数据库、上传文件）
  redis-data:      # Redis 数据持久化
  pansou-data:     # PanSou 缓存

# ── 日志轮转模板 ────────────────────────────────────────────────────────────
x-logging: &default-logging
  driver: json-file
  options:
    max-size: "50m"
    max-file: "10"

# ── 服务定义 ────────────────────────────────────────────────────────────────
services:

  # ═══════════════════════════════════════════════════════════════════════════
  # CloudSearch 主应用 — Node.js + Express + TypeScript
  # ═══════════════════════════════════════════════════════════════════════════
  app:
    container_name: CloudSearch_App

    # ┌─ 开发模式：从源码构建 ──────────────────────────────────────────┐
    build:
      context: ./source_clean
      dockerfile: Dockerfile
    # └────────────────────────────────────────────────────────────────┘

    # ┌─ 生产模式：使用预构建镜像（切换时取消注释下行，注释 build 段）───┐
    # image: gitea.timxx.cn/admin/cloudsearch:latest
    # image: gitea.timxx.cn/admin/cloudsearch:v0.3.32
    # └────────────────────────────────────────────────────────────────┘

    ports:
      - "${PORT:-9527}:9527"

    environment:
      # ── 核心 ──
      - NODE_ENV=${NODE_ENV:-production}
      - PORT=${PORT:-9527}
      - TZ=${TZ:-Asia/Shanghai}

      # ── 安全 ──
      - JWT_SECRET=${JWT_SECRET}
      - ADMIN_USERNAME=${ADMIN_USERNAME:-admin}
      - ADMIN_PASSWORD=${ADMIN_PASSWORD}
      - COOKIE_ENCRYPTION_KEY=${COOKIE_ENCRYPTION_KEY:-}
      - CORS_ORIGIN=${CORS_ORIGIN}

      # ── 数据库 & 缓存 ──
      - DATA_DIR=/data
      - DB_PATH=${DB_PATH:-/data/database.sqlite}
      - REDIS_URL=${REDIS_URL:-redis://redis:6379}

      # ── 搜索服务 ──
      - PANSOU_URL=${PANSOU_URL:-http://pansou:80}
      - PANSOU_AUTH_TOKEN=${PANSOU_AUTH_TOKEN:-}

      # ── 网盘校验 ──
      - VALIDATION_CONCURRENCY=${VALIDATION_CONCURRENCY:-10}
      - VALIDATION_TIMEOUT=${VALIDATION_TIMEOUT:-5000}
      - CACHE_TTL_VALID=${CACHE_TTL_VALID:-14400}
      - CACHE_TTL_INVALID=${CACHE_TTL_INVALID:-3600}

      # ── 路径 ──
      - CHROMIUM_PATH=${CHROMIUM_PATH:-/usr/bin/chromium-browser}
      - APP_VERSION_FILE=${APP_VERSION_FILE:-/data/VERSION}
      - UPLOAD_DIR=${UPLOAD_DIR:-/app/uploads}

      # ── 日志 ──
      - LOG_LEVEL=${LOG_LEVEL:-info}

    volumes:
      - app-data:/data                          # 数据库
      - ./uploads:/app/uploads                  # 上传文件
      - ./VERSION:/app/VERSION                  # 版本文件
      - ./icons:/app/dist/frontend/admin/icons  # 云盘图标

    depends_on:
      redis:
        condition: service_healthy
      pansou:
        condition: service_started

    restart: always
    networks:
      - cloudsearch-network
    logging: *default-logging
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:9527/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  # ═══════════════════════════════════════════════════════════════════════════
  # Redis — 缓存 & 会话
  # ═══════════════════════════════════════════════════════════════════════════
  redis:
    container_name: CloudSearch_Redis
    image: redis:7-alpine
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis-data:/data
    restart: always
    networks:
      - cloudsearch-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 5s

  # ═══════════════════════════════════════════════════════════════════════════
  # PanSou — 第三方搜索聚合
  # ═══════════════════════════════════════════════════════════════════════════
  pansou:
    container_name: CloudSearch_PanSou
    image: ghcr.io/fish2018/pansou-web:latest
    networks:
      cloudsearch-network:
        aliases:
          - pansou
    environment:
      - DOMAIN=${DOMAIN:-localhost}
      - CACHE_TTL=60
    volumes:
      - pansou-data:/app/data
    restart: always
    logging: *default-logging
