# syntax=docker/dockerfile:1
FROM python:3.13-slim

# Install system dependencies required for RDF processing and file decompression
RUN apt-get update && apt-get install -y --no-install-recommends \
    bzip2 \
    gzip \
    curl \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Install uv for fast dependency management
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

# Set working directory
WORKDIR /app

# Copy dependency files first for better layer caching
COPY pyproject.toml ./

# Install Python dependencies using uv
RUN uv pip install --system --no-cache-dir \
    rdflib>=7.0.0 \
    datasets>=2.0.0 \
    huggingface-hub \
    rich>=13.0.0 \
    httpx>=0.25.0 \
    pyarrow>=14.0.0 \
    pandas>=2.0.0 \
    requests>=2.31.0 \
    python-dotenv \
    google-auth \
    google-api-python-client

# Copy application code
COPY scripts/ ./scripts/
COPY src/ ./src/

# Copy the entrypoint script for HF Space mode
COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

# Create directory for dataset processing with proper permissions
RUN mkdir -p /data/dataset_processing && \
    mkdir -p /data/.cache/huggingface

# Create non-root user for security
RUN useradd -m -u 1000 -s /bin/bash appuser && \
    chown -R appuser:appuser /app /data

# Switch to non-root user
USER appuser

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    HF_HOME=/data/.cache/huggingface \
    PATH="/home/appuser/.local/bin:${PATH}"

# Set working directory to data mount point
WORKDIR /app

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD python -c "import sys; sys.exit(0)"

# Entrypoint for HF Space streaming converter
ENTRYPOINT ["/app/entrypoint.sh"]