services:
  frontend:
    image: ${FRONTEND_IMAGE:-yg-ft-frontend-runtime:latest}
    container_name: yg-ft-frontend
    depends_on:
      backend-api:
        condition: service_started
    ports:
      - "${FRONTEND_PORT:-16801}:80"
    environment:
      BACKEND_PROXY_PASS: ${BACKEND_PROXY_PASS:-http://backend-api:8000}
    volumes:
      - ../../frontend/dist:/usr/share/nginx/html:ro
      - ../nginx.conf.template:/etc/nginx/templates/default.conf.template:ro
    command:
      - /bin/sh
      - -c
      - |
        if [ ! -f /usr/share/nginx/html/index.html ]; then
          echo "frontend dist is missing: build frontend first and ensure ../../frontend/dist is mounted";
          ls -la /usr/share/nginx/html;
          exit 1;
        fi;
        envsubst '$$BACKEND_PROXY_PASS' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf;
        nginx -t;
        nginx -g 'daemon off;'
    networks:
      - yg-ft-app
    restart: unless-stopped

  backend-api:
    image: ${BACKEND_API_IMAGE:-yg-ft-backend-api:latest}
    container_name: yg-ft-backend-api
    depends_on:
      redis:
        condition: service_healthy
    expose:
      - "8000"
    ports:
      - "${BACKEND_API_PORT:-17861}:8000"
    environment:
      APP_ENV: ${APP_ENV:-prod}
      APP_NAME: ${APP_NAME:-YG Fine-Tune Platform API}
      MODELTF_ROUTE_PREFIX: ${MODELTF_ROUTE_PREFIX:-/modelTF}
      CORS_ALLOW_ORIGINS: ${CORS_ALLOW_ORIGINS:-http://localhost:16801,http://127.0.0.1:16801}
      DATABASE_URL: ${DATABASE_URL:-postgresql+psycopg://root:8811614287327Leo@www.caoxiaozhu.com:5432/yg_ft}
      REDIS_URL: ${REDIS_URL:-redis://redis:6379/0}
      USE_BUILTIN_POSTGRES: ${USE_BUILTIN_POSTGRES:-false}
      USE_BUILTIN_REDIS: ${USE_BUILTIN_REDIS:-true}
      LOG_LEVEL: ${LOG_LEVEL:-INFO}
      LOG_DIR: ${LOG_DIR:-/opt/yg-ft/logs/backend}
      LOG_FILE_PREFIX: ${LOG_FILE_PREFIX:-backend}
      LOG_ERROR_FILE_PREFIX: ${LOG_ERROR_FILE_PREFIX:-error}
      LOG_MAX_BYTES: ${LOG_MAX_BYTES:-20971520}
      LOG_RETENTION_DAYS: ${LOG_RETENTION_DAYS:-10}
      COMPUTE_API_BASE_URL: ${COMPUTE_API_BASE_URL:-http://compute-api:9100}
      COMPUTE_SERVICE_TOKEN: ${COMPUTE_SERVICE_TOKEN:-change_me}
      FILE_GATEWAY_BASE_URL: ${FILE_GATEWAY_BASE_URL:-http://compute-api:9101}
      COMPUTE_MODE: ${COMPUTE_MODE:-real}
      COMPUTE_STATUS_SYNC_MODE: ${COMPUTE_STATUS_SYNC_MODE:-polling}
      COMPUTE_POLL_INTERVAL_SECONDS: ${COMPUTE_POLL_INTERVAL_SECONDS:-3}
      COMPUTE_POLL_BATCH_SIZE: ${COMPUTE_POLL_BATCH_SIZE:-100}
      COMPUTE_REQUEST_TIMEOUT_SECONDS: ${COMPUTE_REQUEST_TIMEOUT_SECONDS:-5}
      PYTHONPATH: /app
    volumes:
      - ../../backend:/app:ro
      - ../../runtime/app/logs/backend:/opt/yg-ft/logs/backend
      - ../../runtime/app/data:/data/yg-ft
    networks:
      - yg-ft-app
    healthcheck:
      test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/modelTF/health', timeout=3).read()\""]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 20s
    restart: unless-stopped

  # PostgreSQL uses the shared external database configured by DATABASE_URL in docker/app/.env.
  # Keep this local service commented out unless development needs an isolated database again.
  # postgres:
  #   image: postgres:16-alpine
  #   container_name: yg-ft-postgres
  #   environment:
  #     POSTGRES_DB: ${POSTGRES_DB:-yg_ft}
  #     POSTGRES_USER: ${POSTGRES_USER:-yg_ft}
  #     POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-change_me}
  #     PGDATA: /var/lib/postgresql/data/pgdata
  #   volumes:
  #     - postgres_data:/var/lib/postgresql/data
  #     - ../../backend/app/db/sql/001_platform_runtime.sql:/docker-entrypoint-initdb.d/001-platform-runtime.sql:ro
  #   ports:
  #     - "${POSTGRES_PORT:-15432}:5432"
  #   networks:
  #     - yg-ft-app
  #   healthcheck:
  #     test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
  #     interval: 10s
  #     timeout: 5s
  #     retries: 5
  #   restart: unless-stopped

  redis:
    image: redis:7-alpine
    container_name: yg-ft-redis
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis_data:/data
    ports:
      - "${REDIS_PORT:-16379}:6379"
    networks:
      - yg-ft-app
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    restart: unless-stopped

networks:
  yg-ft-app:
    name: yg-ft-app

volumes:
  # postgres_data:
  redis_data:
