version: "3.8"

services:
  postgres:
    image: postgres:16-alpine
    container_name: notify-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-notify}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
      POSTGRES_DB: ${POSTGRES_DB:-notify}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    # 生产环境不对外暴露数据库端口
    expose:
      - "5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-notify} -d ${POSTGRES_DB:-notify}"]
      interval: 5s
      timeout: 5s
      retries: 5

  backend:
    build:
      context: ./backend_rust
      dockerfile: Dockerfile
    container_name: notify-backend
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      DATABASE_URL: postgresql://${POSTGRES_USER:-notify}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-notify}
      JWT_SECRET: ${JWT_SECRET:?JWT_SECRET is required}
      BASE_URL: ${BASE_URL:-https://notify.example.com}
    ports:
      - "127.0.0.1:4000:4000"
    volumes:
      - uploads_data:/app/uploads

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        # 构建时注入 API URL（用于 Next.js 静态优化）
        NEXT_PUBLIC_API_BASE: ${NEXT_PUBLIC_API_BASE:-}
    container_name: notify-frontend
    restart: unless-stopped
    depends_on:
      - backend
    environment:
      # 运行时 API URL（用于 SSR）
      NEXT_PUBLIC_API_BASE: ${NEXT_PUBLIC_API_BASE:-}
    ports:
      - "127.0.0.1:3000:3000"

volumes:
  postgres_data:
  uploads_data:
