# --- Stage 1: Build (Cài đặt thư viện) ---
FROM python:3.9-slim AS builder

WORKDIR /build

# Khởi tạo môi trường ảo để tách biệt thư viện
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

COPY requirements.txt .
RUN pip install --upgrade --no-cache-dir -r requirements.txt


# --- Stage 2: Runtime (Chỉ chạy Prod) ---
FROM python:3.9-slim

WORKDIR /app

# Chỉ copy môi trường ảo đã build xong từ Stage 1
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Copy mã nguồn
COPY main.py .

# Tạo folder lưu ảnh và phân quyền (tăng tính bảo mật)
RUN mkdir images && chmod 777 images

# Chạy bằng Uvicorn với chế độ production (tắt reload)
# Port 80 theo yêu cầu trước đó của bạn
EXPOSE 80
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80", "--workers", "4"]