# Build stage
FROM python:3.12-slim as builder

WORKDIR /app

# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Copy project files
COPY pyproject.toml README.md LICENSE ./
COPY src/ ./src/

# Build wheel
RUN pip install --no-cache-dir build && \
    python -m build --wheel

# Runtime stage
FROM python:3.12-slim

# Install restic
RUN apt-get update && apt-get install -y --no-install-recommends \
    restic \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/* \
    && restic version

# Create non-root user
RUN groupadd -r backup && useradd -r -g backup backup

# Copy and install wheel
WORKDIR /app
COPY --from=builder /app/dist/*.whl ./
RUN pip install --no-cache-dir *.whl && rm *.whl

# Create config directories
RUN mkdir -p /etc/restic-backup /var/backups && \
    chown -R backup:backup /var/backups

# Switch to non-root user
USER backup

# Default command
ENTRYPOINT ["restic-backup"]
CMD ["--help"]
