version: '3.8'

services:
  mysql:
    image: mysql:8.0
    container_name: rent-prod-mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${PROD_DB_PASSWORD:-rent123456}
      MYSQL_DATABASE: rent_platform
      MYSQL_CHARSET: utf8mb4
      MYSQL_COLLATION: utf8mb4_unicode_ci
    ports:
      - "3306:3306"
    volumes:
      - mysql_prod_data:/var/lib/mysql
      - ../../database/migrations:/docker-entrypoint-initdb.d
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - rent-prod

  redis:
    image: redis:7-alpine
    container_name: rent-prod-redis
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - redis_prod_data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - rent-prod

  server:
    build:
      context: ../..
      dockerfile: deploy/docker/Dockerfile.server
    container_name: rent-prod-server
    restart: always
    ports:
      - "10080:3000"  # 宝塔反向代理端口
    environment:
      NODE_ENV: production
      PORT: 3000
      # Docker 内部网络
      DB_HOST: mysql
      DB_PORT: 3306
      DB_USERNAME: root
      DB_DATABASE: rent_platform
      REDIS_HOST: redis
      REDIS_PORT: 6379
      # CI 注入的基础密钥
      DB_PASSWORD: ${PROD_DB_PASSWORD}
      JWT_SECRET: ${PROD_JWT_SECRET}
      # 加密密钥（用于解密数据库中的业务密钥）
      ENCRYPTION_KEY: ${ENCRYPTION_KEY}
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - rent-prod

  merchant-admin:
    build:
      context: ../..
      dockerfile: deploy/docker/Dockerfile.merchant
    container_name: rent-prod-merchant
    restart: always
    ports:
      - "10082:80"  # 宝塔反向代理端口
    depends_on:
      - server
    networks:
      - rent-prod

  platform-admin:
    build:
      context: ../..
      dockerfile: deploy/docker/Dockerfile.platform
    container_name: rent-prod-platform
    restart: always
    ports:
      - "10084:80"  # 宝塔反向代理端口
    depends_on:
      - server
    networks:
      - rent-prod

  website:
    build:
      context: ../..
      dockerfile: deploy/docker/Dockerfile.website
    container_name: rent-prod-website
    restart: always
    ports:
      - "10086:80"  # 宝塔反向代理端口
    networks:
      - rent-prod

volumes:
  mysql_prod_data:
  redis_prod_data:

networks:
  rent-prod:
    name: rent-prod
