version: '3.8'

services:
  mysql:
    image: mysql:8.0
    container_name: rent-test-mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${TEST_DB_PASSWORD:-rent123456}
      MYSQL_DATABASE: rent_platform
      MYSQL_CHARSET: utf8mb4
      MYSQL_COLLATION: utf8mb4_unicode_ci
    ports:
      - "3307:3306"
    volumes:
      - mysql_test_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-test

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

  server:
    build:
      context: ../..
      dockerfile: deploy/docker/Dockerfile.server
    container_name: rent-test-server
    restart: always
    ports:
      - "10081:3000"  # 宝塔反向代理端口
    environment:
      NODE_ENV: test
      PORT: 3000
      DB_HOST: mysql
      DB_PORT: 3306
      DB_USERNAME: root
      DB_DATABASE: rent_platform
      REDIS_HOST: redis
      REDIS_PORT: 6379
      DB_PASSWORD: ${TEST_DB_PASSWORD}
      JWT_SECRET: ${TEST_JWT_SECRET}
      ENCRYPTION_KEY: ${ENCRYPTION_KEY}
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - rent-test

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

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

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

volumes:
  mysql_test_data:
  redis_test_data:

networks:
  rent-test:
    name: rent-test
