FROM docker:27-cli AS docker-cli

FROM python:3.11-slim

# Copy Docker CLI only (no full docker daemon needed — we talk to host socket)
COPY --from=docker-cli /usr/local/bin/docker /usr/local/bin/docker

WORKDIR /app

# UID/GID of the host user that owns crontab entries, and the GID of the
# host's `crontab` group that owns /var/spool/cron/crontabs. These must
# match the host so the `crontab` CLI can read/write the bind-mounted
# spool directory. Override at build time if the host differs.
ARG CRONTAB_HOST_UID=1000
ARG CRONTAB_HOST_GID=1000
ARG CRONTAB_GROUP_GID=997
ARG CRONTAB_HOST_USER=me

RUN groupadd -g ${CRONTAB_HOST_GID} ${CRONTAB_HOST_USER} \
    && groupadd -g ${CRONTAB_GROUP_GID} crontab \
    && useradd -m -u ${CRONTAB_HOST_UID} -g ${CRONTAB_HOST_GID} -G crontab -s /bin/bash ${CRONTAB_HOST_USER}

# Minimal system deps: gcc (for any C extensions), curl (healthcheck),
# and cron (provides the `crontab` CLI used to read/write the host's
# crontab, whose spool directory is bind-mounted into this container).
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    curl \
    cron \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first for better layer caching
COPY requirements.txt .
RUN pip install --no-cache-dir \
    --default-timeout=200 \
    --retries 10 \
    -r requirements.txt

# Copy application code
COPY . .

# Create data directories
RUN mkdir -p /data/cache /data/models

EXPOSE 8001

CMD ["python", "-m", "uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8001"]
