services:
  app:
    build:
      context: .
      target: runner
    restart: unless-stopped
    ports:
      - "${APP_PORT:-3000}:3000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    env_file: .env
    environment:
      - DATABASE_URL=postgresql://jboard:${POSTGRES_PASSWORD:-jboard123}@db:5432/jboard
      - REDIS_URL=redis://redis:6379
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:3000/api/public/app-info || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  # 数据库初始化：首次部署或 schema 变更时运行
  # docker compose run --rm init
  init:
    build:
      context: .
      target: init
    depends_on:
      db:
        condition: service_healthy
    env_file: .env
    environment:
      - DATABASE_URL=postgresql://jboard:${POSTGRES_PASSWORD:-jboard123}@db:5432/jboard
    profiles:
      - setup

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: jboard
      POSTGRES_USER: jboard
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-jboard123}
    # 仅暴露给内部网络，生产环境不要对外开放
    # ports:
    #   - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U jboard"]
      interval: 5s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    volumes:
      - redis_data:/data
    # 仅暴露给内部网络，生产环境不要对外开放
    # ports:
    #   - "6379:6379"
    healthcheck:
      test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:
  redis_data:
