# Backend Dockerfile for ljwx-stock
FROM python:3.12-slim

LABEL maintainer="ljwx-stock"
LABEL description="Stock recommendation system backend"

# 设置工作目录
WORKDIR /app

# 安装系统依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    g++ \
    libpq-dev \
    curl \
    netcat-openbsd \
    && rm -rf /var/lib/apt/lists/*

# 配置pip使用国内镜像源
RUN pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple

# 复制requirements文件
COPY requirements.txt .

# 安装Python依赖
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt || \
    (grep -v "^#\|^pytest\|^black\|^flake8\|^mypy\|^isort\|^ipython\|^jupyter" requirements.txt > requirements-prod.txt && \
     pip install --no-cache-dir -r requirements-prod.txt)

# 复制应用代码
COPY . .

# 创建日志目录
RUN mkdir -p logs

# 暴露端口
EXPOSE 8000

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=90s --retries=3 \
    CMD curl -f http://localhost:8000/health || exit 1

# 启动命令
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
