# 美家卡智影 API - Docker 镜像
# ===========================================
# 配置 Docker 镜像加速后，基础镜像 python:3.13-slim 会自动走加速源

FROM python:3.13-slim AS builder

ENV UV_COMPILE_BYTECODE=1 \
    UV_LINK_MODE=copy \
    UV_PYTHON_DOWNLOADS=never

WORKDIR /app

# 安装 uv（pip 走 PyPI，通常国内服务器能访问；如果也慢可换阿里源）
RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ uv

# 先复制锁定文件，利用 Docker 缓存层
COPY requirements.lock pyproject.toml ./

# 创建虚拟环境并安装依赖
RUN uv venv /opt/venv && \
    uv pip sync --index-url https://mirrors.aliyun.com/pypi/simple/ --python /opt/venv/bin/python requirements.lock

# 复制应用代码并安装
COPY app/ ./app/
RUN uv pip install --index-url https://mirrors.aliyun.com/pypi/simple/ --python /opt/venv/bin/python --no-deps -e .

# ===== 生产镜像 =====
FROM python:3.13-slim AS production

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PATH="/opt/venv/bin:$PATH"

# 从 builder 复制虚拟环境
COPY --from=builder /opt/venv /opt/venv

WORKDIR /app

# 复制应用代码和配置
COPY app/ ./app/
COPY config/ ./config/
COPY scripts/ ./scripts/
COPY alembic.ini .
COPY alembic/ ./alembic/
COPY pyproject.toml .

EXPOSE 8000

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--no-access-log"]
